2

我有以下場景。Javascript set window.name - IE中的問題

網站A被打開使用網站B window.open("Website B", "windowName");

在網站BI有以下代碼:

<script> 
    window.name=''; 
    window.location.href = 'Website C'; 
</script> 

在網站CI具有在Firefox和鉻(所有版本)window.name等於 '',但在IE(版本9,10,11)中它等於'windowName'。

有人可以解釋爲什麼嗎?當我到達網站C時,我需要一個解決方法總是有window.name = ''。我不能在網站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> 
+0

添加了證明該問題並要求某人提供解決方法的源代碼。請重新打開。 –

+0

無法重現問題(使用各種瀏覽器,包括MSIE6),雖然在我的情況下,我設置名稱/在單獨的Javascript調用中調用重定向而不是同步 - 是否會在您通過setTimeout調用更改document.location後發生改名字? – symcbean

+0

在頁面C上的IE(所有版本9-11)上,它仍然在頁面B上添加類似內容後顯示window.name ='某個窗口名稱':setTimeout(doRedirect,10000);請注意,我只能訪問頁面B的代碼,所以頁面A和頁面C我無法改變來解決這個問題。 –

回答

4

根據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/

因此,您的代碼可能存在其他問題。

+1

確實在網站B上,設置了window.name =''後,alert(window.name)顯示爲空字符串,但重定向之後,window.name被設置回原來的值。所以在網站C上,它又等於'windowName'。我需要避免。 –

+0

我已經更新了網站a,b和c的源代碼的初始文章,它顯示了IE和Mozilla的問題。任何人都可以解釋這種行爲並提供解決方法? –

0

我有同樣的問題,發現你需要重新命名窗口後,您設置新的window.location。這對我有效!

+0

這並沒有真正回答這個問題。如果您有不同的問題,可以通過單擊[提問](http://stackoverflow.com/questions/ask)來提問。您還可以[添加賞金](http://stackoverflow.com/help/privileges/set-bounties)在您擁有足夠的[聲譽](http://stackoverflow.com/help/)時吸引更多人關注此問題什麼聲譽)。 - [來自評論](/ review/low-quality-posts/14281982) – Liam

+0

爲什麼不呢?您可以閱讀現有問題的評論,即在更改其位置後,將window.name重置爲正式名稱。這可以通過在window.location更改後(或在本例中爲document.location)設置window.name屬性來解決。這種方式即使在位置改變之後窗口仍會保留新名稱。 –

+0

你能提供源代碼嗎?如果我更改document.location,則重定向已完成。如果地點不在我們的控制範圍內,我如何更改名稱? –