Javascript 设置 window.name - IE 中的问题

标签 javascript internet-explorer variables window global-variables

我有以下场景。

网站 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/

我还尝试将其更改为空字符串,并且有效:

http://jsfiddle.net/5cBBy/2/

因此,您的代码可能还有其他问题。

关于Javascript 设置 window.name - IE 中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12645374/

相关文章:

javascript - 从 reviver 访问 JSON 对象中的数组

javascript - 为什么 .add() 不在列中插入值?

javascript - 如何让 jquery.couch.app.js 与 IE8 一起工作

php - 使用 HTML 表单将 Smarty 变量传递给 php 文件。

c++ - 如何 #define 多个值 C/C++

javascript - 将 javascript 变量传递给同一 php 文件中的 php 变量

javascript - 使用 javascript 和 jquery 向每一行添加按钮

javascript - 缩短纯 div 文本

ruby - WebDriver 在测试后关闭 Internet Explorer

internet-explorer - Internet Explorer 和 TLS1.2 : website down