Javascript 设置 window.name - IE 中的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12645374/
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 set window.name - issue in IE
提问by Stefan Iancu
I have the following scenario.
我有以下场景。
Website A is opening Website B using window.open("Website B", "windowName");
网站 A 正在打开网站 B 使用 window.open("Website B", "windowName");
In Website B I have the following code:
在网站 BI 中有以下代码:
<script>
window.name='';
window.location.href = 'Website C';
</script>
In Website C I have in Firefox and Chrome (all versions) window.name
equal to '', but in IE(versions 9, 10, 11) it is equal to 'windowName'.
在网站 CI 中,Firefox 和 Chrome(所有版本)window.name
等于 '',但在 IE(版本 9、10、11)中它等于 'windowName'。
Can someone explain why? I need an workaround to always have window.name = ''
when I get to Website C. I cannot use windows.open in Website B to open Website C, I need to use window.location.
有人可以解释为什么吗?window.name = ''
当我到达网站 C 时,我需要一种解决方法。我无法在网站 B 中使用 windows.open 打开网站 C,我需要使用 window.location。
Source coded added:
添加源代码:
index.html (Site A)
index.html(站点 A)
<!DOCTYPE html>
<html>
<title>Page A</title>
<head>
<script>
function test2(){
window.open("index2.html","Some window name","width=500,height=500");
}
</script>
</head>
<body>
<input type="button" onClick="test2();">
</body>
</html>
index2.html (Site B)
index2.html(站点 B)
<!DOCTYPE html>
<html>
<title>Page B</title>
<head>
<script>
document.write("initial window name: [" + window.name + "]<br/><br/>");
window.name=""; //we set it to empty string
document.write("after we set window.name to empty string: [" + window.name + "]"); //all fine in all browsers, shows nothing
document.location= 'index3.html';
</script>
</head>
<body>
</body>
</html>
index3.html (Site C)
index3.html(站点 C)
<!DOCTYPE html>
<html>
<title>Page C</title>
<head>
<script>
document.write("initial window name: [" + window.name + "]"); //OK in Firefox (shows nothing). Not OK in IE, shows "Some window name"
</script>
</head>
<body>
</body>
</html>
回答by Guffa
According to the MSDN documentation, the name
property is changeable:
根据 MSDN 文档,该name
属性是可变的:
http://msdn.microsoft.com/en-us/library/ie/ms534187%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/ie/ms534187%28v=vs.85%29.aspx
I tried to change the name
property, and it works fine in IE9:
我试图更改该name
属性,它在 IE9 中运行良好:
http://jsfiddle.net/Guffa/5cBBy/1/
http://jsfiddle.net/Guffa/5cBBy/1/
I also tried to change it to an empty string, and that works:
我还尝试将其更改为空字符串,并且有效:
So, there is likely something else that is wrong with your code.
因此,您的代码可能还有其他问题。
回答by X-Hale
I had the same problem and found out that you need to rename the window AFTER you set the new window.location. That worked for me!
我遇到了同样的问题,发现在设置新的 window.location 后需要重命名窗口。那对我有用!