窗口对象上的 javascript 函数在调用时会替换该窗口的内容吗?(有时??)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5628257/
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
javascript function on a window object, when invoked, replaces that window's contents? (sometimes??)
提问by Dan Burton
For reasons which shall not be enumerated here, I've found it potentially useful to attach functions to the window object. However, I've discovered rather weird behavior.
出于此处不一一列举的原因,我发现将函数附加到 window 对象可能很有用。但是,我发现了相当奇怪的行为。
<html><head><script>
function sideEffect() { console.log("Side effect happened. Wewt."); }
window.foo = function() {
sideEffect();
return true;
}
window.bar = function() {
sideEffect();
}
</script></head>
<body>
<a href="javascript:window.foo();">Replaces entire window with "true"</a>
<br />
<a href="javascript:window.bar();">Doesn't</a>
</body></html>
Why exactly does invoking a function with a return value decide to replace the window's contents? This happens in Firefox and Opera, but not in IE9, Chrome, or Safari (all tested on Win7).
为什么调用具有返回值的函数会决定替换窗口的内容?这发生在 Firefox 和 Opera 中,但不发生在 IE9、Chrome 或 Safari(均在 Win7 上测试)中。
So the question is this: Is there some sort of documentation that specifies this behavior? Or is this a (known) bug in FF/Opera?
所以问题是:是否有某种文档指定了这种行为?或者这是 FF/Opera 中的(已知)错误?
[edit] Interestingly (according to answers and comments thusfar) it appears that the abuse of the window
object is a red herring here.
[编辑] 有趣的是(根据迄今为止的答案和评论),似乎滥用该window
对象在这里是一种红鲱鱼。
回答by KP Taylor
Your code is working perfectly--the browser is doing exactly what you are telling it to do.
您的代码运行良好——浏览器完全按照您的要求执行。
JavaScript lines are valid in the URL bar of web browsers, and the browser will execute them immediately. (Try it: write an alert box in the URL bar, hit enter, and see what happens.) By writing your JavaScript into the href
of your anchor tags, you are setting the location of the browser (in other words, the URL) to that line of JavaScript. Because one of your functions returns a value, the document is over written by that value; this is normal browser behavior.
JavaScript 行在 Web 浏览器的 URL 栏中是有效的,浏览器会立即执行它们。(试试看:在 URL 栏中写一个警告框,按 Enter,看看会发生什么。)通过将 JavaScript 写入href
锚标记的 ,您将浏览器的位置(换句话说,URL)设置为那一行 JavaScript。因为您的一个函数返回一个值,该文档被该值覆盖;这是正常的浏览器行为。
回答by Mark Kahn
just put void(0);
after your calls:
只需void(0);
在您的电话后放:
<a href="javascript:window.foo();void(0);">
If it still does that (I don't think it will), add an `|| false':
如果它仍然这样做(我认为不会),请添加一个`|| 错误的':
<a href="javascript:window.foo()||0;void(0);">
And ignore the "don't do that" comments -- there's nothing wrong with what you're doing, but all the window.
's are pointless (everything is in window
anyway)
并忽略“不要那样做”的评论——你在做什么没有错,但所有window.
的都是毫无意义的(window
无论如何,一切都在)
回答by Pointy
Well, it's really bad form to use "javascript:" URLs like that anyway. Create an "onclick" attribute if you absolutely must bind event handlers that way, or (better) do it with unobtrusive code.
好吧,无论如何使用“javascript:”这样的 URL 真的很糟糕。如果您绝对必须以这种方式绑定事件处理程序,则创建一个“onclick”属性,或者(更好)使用不显眼的代码来执行此操作。
That behavior is pretty old; the browsers that do it have been behaving that way since ancient times.
这种行为已经很老了;自古以来,执行此操作的浏览器就一直以这种方式运行。