firefox javascript return false in href 重定向浏览器并显示 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10949016/
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
firefox javascript return false in href redirects the browser and displays false
提问by Jayapal Chandran
following is my code working well in chrome.
以下是我在 chrome 中运行良好的代码。
<body>
<a href="javascript: sam.save();">hehe</a>
<script>
var sam = {
save : function()
{
alert("here")
return false;
}
}
</script>
when in run in firefox the page redirects and false is displayed on the screen with the adress bar content like in the picture
在 firefox 中运行时,页面重定向,错误显示在屏幕上,地址栏内容如图所示
firefox version is 9.0.1
火狐版本是 9.0.1
suggestions and circumvents please...
建议和规避请...
采纳答案by Boris Zbarsky
The code you cite can't possibly produce the behavior you observe. The observed behavior would only happen if sam.save()
returned false
, whereas the quoted code returns undefined
. What does your actual complete code look like?
您引用的代码不可能产生您观察到的行为。观察到的行为只会在sam.save()
返回时发生false
,而引用的代码返回undefined
。您实际的完整代码是什么样的?
Edit:The useful answer was in a comment. I put it here to make it easier to find.
编辑:有用的答案在评论中。我把它放在这里是为了方便查找。
Oh, I missed the "return false" after the alert. In that case, the behavior you see is correct: if the javascript: execution returns a value other than undefined that value is treated as a string of HTML and rendered. At least in most browsers. – Boris Zbarsky Jun 11 at 15:25
哦,我错过了警报后的“返回假”。在这种情况下,您看到的行为是正确的:如果 javascript: 执行返回 undefined 以外的值,则该值被视为 HTML 字符串并呈现。至少在大多数浏览器中。– 鲍里斯·兹巴尔斯基 6 月 11 日 15:25
回答by boateng
For some reason return false doesn't work in FF inside href="javascript:", but void(0) does.
出于某种原因, return false 在 href="javascript:" 内的 FF 中不起作用,但 void(0) 起作用。
<a href="javascript: sam.save();void(0);">hehe</a>
回答by Lee
<a href="#" onclick="sam.save();">hehe</a>
回答by Jerry Liang
The more compatible syntax should be
更兼容的语法应该是
<a href="javascript:void(0)" onclick="sam.save()">hehe</a>
回答by Hesam Farhang
try it
试试看
`<a href="javascript: void(sam.save())">hehe</a>`
Hope this helps...
希望这可以帮助...
回答by Krzysztof Weso?owski
Example how it works:
例如它是如何工作的:
Firefox: Second and third is rendering a white page
Firefox:第二和第三是渲染白页
Chromium: The first case is rendering the white page
Chromium:第一种情况是渲染白页
<body>
<a href="javascript: something.with_early_return();">with_early_return</a>
<a href="javascript: something.with_false();">with_false</a>
<a href="javascript: something.with_string();">with_string</a>
<script>
var something = {
with_early_return: function() {
alert("with_early_return");
return;
},
with_false: function () {
alert('with_false');
return false;
},
with_string: function () {
alert('with_string');
return 'It renders this as text';
}
}
</script>