我有以下场景。
网站 A 正在使用 window.open("Website B", "windowName");
打开网站 B
在网站 B 中我有以下代码:
<script>
window.name='';
window.location.href = 'Website C';
</script>
在网站 C 中,我在 Firefox 和 Chrome(所有版本)中都有 window.name
等于 '',但在 IE(版本 9、10、11)中它等于 'windowName'。
有人能解释一下为什么吗?我需要一个解决方法来始终拥有 window.name = ''
当我到达网站C时,我无法在网站B中使用windows.open打开网站C,我需要使用window.location。
添加源代码:
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(站点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(站点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>
最佳答案
根据 MSDN 文档,name
属性是可以更改的:
http://msdn.microsoft.com/en-us/library/ie/ms534187%28v=vs.85%29.aspx
我尝试更改 name
属性,它在 IE9 中工作正常:
http://jsfiddle.net/Guffa/5cBBy/1/
我还尝试将其更改为空字符串,并且有效:
因此,您的代码可能还有其他问题。
关于Javascript 设置 window.name - IE 中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12645374/