javascript Safari 没有打开带有 window.location.href 的 href
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9009836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Safari not opening a href with window.location.href
提问by user1170040
Safari ignores the a href link. Other browsers pass the countClicks function AND pass the link to open the new browser window. Any suggestions?
Safari 忽略 a href 链接。其他浏览器传递 countClicks 函数并传递链接以打开新的浏览器窗口。有什么建议?
The JavaScript source is as follows:
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>
Whereas the HTML markup is:
而 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>
采纳答案by Shankar Damodaran
Try with either document.location.href
or just location.href
.
尝试使用document.location.href
或仅使用location.href
.
Hey there. Your code should work. Well i tried this simple example on Safari browser and it works good. Try yourself.
嘿。您的代码应该可以工作。好吧,我在 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 :FYI : I have used window.open
锚属性:仅供参考:我用过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>
Passing variables from function
从函数传递变量
<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>
回答by Waqas Malik
Remove the url from hrefand 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 :
从href 中删除 url并将该 url 传递给您在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;
}
and here is your anchor tag :
这是你的锚标签:
< 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>
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.
因此,此代码将打开一个新选项卡(使用您提供的 url),打开选项卡后,window.location 将照常工作。希望能帮到你。