Javascript window.location.href 在 Safari 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4210995/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:24:15  来源:igfitidea点击:

window.location.href not working in Safari

javascripthtmlsafari

提问by 99miles

Why won't this work in Safari?

为什么这在 Safari 中不起作用?

<script language="javascript" type="text/javascript">
function changeUrl(url) {
  window.location.href='http://google.com';
  return false;
}
</script>

<form action="#" onsubmit="changeUrl(this)" />
  <input type="Submit" value="Go" />
</form>

回答by Paul Kehrer

Safari appears to dislike having the return false occur in the function call. If you move it into your onsubmit as onsubmit="openPop(this.action);return false;" then Safari will work without issue.

Safari 似乎不喜欢在函数调用中出现 return false。如果你把它移到你的 onsubmit 中作为 onsubmit="openPop(this.action);return false;" 那么 Safari 就可以正常工作了。

Edit: To improve the answer, onsubmit itself needs to return false, so openPop returning false is not enough. You could just have it do onsubmit="return openPop(this.action);" though.

编辑:为了改进答案,onsubmit 本身需要返回 false,所以 openPop 返回 false 是不够的。你可以让它做 onsubmit="return openPop(this.action);" 尽管。

回答by Blake

Maybe this will work:

也许这会奏效:

<script>
function popitup(url) {
    newwindow=window.open(url,'name','height=200,width=150');
    if (window.focus) {newwindow.focus()}
    return false;
}

// -->
</script>

Then use this HTML:

然后使用这个 HTML:

<a href="popupex.html" onclick="return popitup('popupex.html')">Link to popup</a>

回答by IBarnes

I was able to get it to work in Safari using this code.

我能够使用此代码使其在 Safari 中工作。

if (app.Name == "Safari") {
window.location ="your-url-here"
}