Safari没有使用window.location.href打开href

时间:2012-01-25 20:33:04

标签: javascript safari

Safari会忽略a href链接。其他浏览器传递countClicks函数并传递链接以打开新的浏览器窗口。有什么建议吗?

JavaScript源代码如下:

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

HTML标记为:

<a href="http://www.polclients.com" target="_blank" onclick="countClicks('POL','POL',6808,387,'www.princetonol.com','/index.cfm','x=x&at=no')">Link</a>

2 个答案:

答案 0 :(得分:0)

尝试使用document.location.hreflocation.href

嘿那里。你的代码应该有效。我在Safari浏览器上尝试了这个简单的例子,它运行良好。试试吧。

<html>
<head>
<script language="JavaScript" type="text/JavaScript">
    function countClicks()
         { 
           window.location.href = "http://www.stackoverflow.com";
         }
</script>
</head>
<body onLoad=countClicks()>
</body>
</html>

Anchor Property:仅供参考:我使用过window.open

<html>
<head>
<script language="JavaScript" type="text/JavaScript">
    function countClicks()
     { 
      window.open("http://stackoverflow.com", "_blank");
     }
</script>
</head>
<body>
<a href="#" name="xy" onclick="javascript:countClicks(this);">Visit StackOverflow Website</a>
</body>
</html>

从函数传递变量

<html>
<head>
<script language="JavaScript" type="text/JavaScript">
    function countClicks(a,b)
    { 
       window.open("http://stackoverflow.com?id="+a+"&id2="+b, "_blank");
    }
</script>
</head>
<body>
<a href="#" name="xy" onclick="javascript:countClicks(2,3);">test</a>
</body>
</html>

答案 1 :(得分:-1)

href 中移除网址,然后将网址传递给您在 onclick 上调用的功能。
在此功能中,您只需使用window.open(your_url)打开新选项卡中的链接即可 所以,这将解决你的问题 以下是样本:

<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;
}  

这是您的锚标记:

< a href="javascript:void(0)" onclick="countClicks('POL','POL',6808,387,'www.princetonol.com','/index.cfm','x=x&at=no','http://www.polclients.com')">Link< /a>  

因此,此代码将打开一个新标签(使用您提供的网址) 打开选项卡后,window.location将照常工作。 希望,这对你有所帮助。