Safari ignores the a href link. Other browsers pass the countClicks function AND pass the link to open the new browser window. Any suggestions?
The JavaScript source is as follows:
<script language="JavaScript" type="text/JavaScript">
function countClicks(enterprise_code,sid_code,buspart_id,prod_id,server_name,path_info,query_string)
{
window.location.href = "/sharedInc/cf/polTrack.cfm?Buspart_id=" + buspart_id + "&Enterprise_Code=" + enterprise_code + "&Sid_Code=" + sid_code + "&Prod_id=" + prod_id + "&Server_Name=" + server_name + "&Path_Info=" + path_info + "&Query_String=" + query_string;
}
</script>
Whereas the HTML markup is:
<a href="" target="_blank" onclick="countClicks('POL','POL',6808,387,'www.princetonol','/index.cfm','x=x&at=no')">Link</a>
Safari ignores the a href link. Other browsers pass the countClicks function AND pass the link to open the new browser window. Any suggestions?
The JavaScript source is as follows:
<script language="JavaScript" type="text/JavaScript">
function countClicks(enterprise_code,sid_code,buspart_id,prod_id,server_name,path_info,query_string)
{
window.location.href = "/sharedInc/cf/polTrack.cfm?Buspart_id=" + buspart_id + "&Enterprise_Code=" + enterprise_code + "&Sid_Code=" + sid_code + "&Prod_id=" + prod_id + "&Server_Name=" + server_name + "&Path_Info=" + path_info + "&Query_String=" + query_string;
}
</script>
Whereas the HTML markup is:
<a href="http://www.polclients." target="_blank" onclick="countClicks('POL','POL',6808,387,'www.princetonol.','/index.cfm','x=x&at=no')">Link</a>
Share
Improve this question
edited Jan 25, 2012 at 20:34
user529758
asked Jan 25, 2012 at 20:33
user1170040user1170040
231 gold badge1 silver badge6 bronze badges
1
- tried "location.href" without the "window" part? Else try inserting an alert("hello"); or whatever inside the function to see if it even gets a call. Best regards Jonas – Jonas m Commented Jan 25, 2012 at 20:34
2 Answers
Reset to default 1Try with either document.location.href
or just location.href
.
Hey there. Your code should work. Well i tried this simple example on Safari browser and it works good. Try yourself.
<html>
<head>
<script language="JavaScript" type="text/JavaScript">
function countClicks()
{
window.location.href = "http://www.stackoverflow.";
}
</script>
</head>
<body onLoad=countClicks()>
</body>
</html>
Anchor Property : FYI : I have used window.open
<html>
<head>
<script language="JavaScript" type="text/JavaScript">
function countClicks()
{
window.open("http://stackoverflow.", "_blank");
}
</script>
</head>
<body>
<a href="#" name="xy" onclick="javascript:countClicks(this);">Visit StackOverflow Website</a>
</body>
</html>
Passing variables from function
<html>
<head>
<script language="JavaScript" type="text/JavaScript">
function countClicks(a,b)
{
window.open("http://stackoverflow.?id="+a+"&id2="+b, "_blank");
}
</script>
</head>
<body>
<a href="#" name="xy" onclick="javascript:countClicks(2,3);">test</a>
</body>
</html>
Remove the url from href and pass the url to the function which you are calling on onclick.
Inside this function, you can simply use window.open(your_url) to open the link in new tab.
So, this will solve your problem.
Here is the sample :
<script language="JavaScript" type="text/JavaScript">
function countClicks(enterprise_code,sid_code,buspart_id,prod_id,server_name,path_info,query_string,new_site_url)
{
window.open(new_site_url);
window.location.href = "/sharedInc/cf/polTrack.cfm?Buspart_id=" + buspart_id + "&Enterprise_Code=" + enterprise_code + "&Sid_Code=" + sid_code + "&Prod_id=" + prod_id + "&Server_Name=" + server_name + "&Path_Info=" + path_info + "&Query_String=" + query_string;
}
and here is your anchor tag :
< a href="javascript:void(0)" onclick="countClicks('POL','POL',6808,387,'www.princetonol.','/index.cfm','x=x&at=no','http://www.polclients.')">Link< /a>
So, this code will open a new tab (with the url you provide) and after opening the tab, window.location will work as usual. Hope, this will help you.