javascript - Safari not opening a href with window.location.href - Stack Overflow

Safari ignores the a href link. Other browsers pass the countClicks function AND pass the link to open

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
Add a ment  | 

2 Answers 2

Reset to default 1

Try 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745224266a4617388.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信