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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:45:39  来源:igfitidea点击:

Javascript set window.name - issue in IE

javascriptinternet-explorervariableswindowglobal-variables

提问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.nameequal 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 nameproperty 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 nameproperty, 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:

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

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

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

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 后需要重命名窗口。那对我有用!