Javascript set window.name - issue in IE

12,663

Solution 1

According to the MSDN documentation, the name property is changeable:

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:

http://jsfiddle.net/Guffa/5cBBy/1/

I also tried to change it to an empty string, and that works:

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

So, there is likely something else that is wrong with your code.

Solution 2

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!

Share:
12,663
Stefan Iancu
Author by

Stefan Iancu

CTO

Updated on June 04, 2022

Comments

  • Stefan Iancu
    Stefan Iancu almost 2 years

    I have the following scenario.

    Website A is opening Website B using window.open("Website B", "windowName");

    In Website B I have the following code:

    <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'.

    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.

    Source coded added:

    index.html (Site 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)

    <!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)

    <!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>
    
  • Stefan Iancu
    Stefan Iancu over 11 years
    Indeed on Website B, after I set up window.name = '', alert(window.name) it shows empty string, but after the redirect somehow window.name is set back to its original value. So on Website C it is again equal to 'windowName'. Which I need to avoid.
  • Stefan Iancu
    Stefan Iancu over 10 years
    I have updated the initial post with source code for site a,b and c which shows what the problem is on IE vs. Mozilla. Anyone can explain this behavior and provide an workaround?
  • Liam
    Liam over 7 years
    This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review
  • X-Hale
    X-Hale over 7 years
    Why not? You can read in the comments that the existing problem is, that the window.name is reset to the oringinal name after changing its location. This can be fixed by setting the window.name property right after the change of window.location (or in this case document.location). That way the window will keep the new name even after the location change.
  • Stefan Iancu
    Stefan Iancu over 7 years
    Can you provide the source code? If I change the document.location the redirection is already done. How can I change the name then if the location is not under our control?
  • X-Hale
    X-Hale over 7 years
    Sure, I did it simply that way:
  • X-Hale
    X-Hale over 7 years
    'function NavigateHome() { window.location.assign("MyNewLocation.aspx"); window.name = "MyNewName" + new Date().toISOString(); }'