Search code examples
javascript.netinternet-explorerinternet-explorer-8antixsslibrary

IE8 window.open name - doesn't like JavaScript encoding?


I'm calling window.open() like this:

window.open('blank.html', 'New_Window\x3a_Jamie', 'width=800,height=800');

What I've done in the code is taken the window's name and JavaScript encoded it using the Microsoft Web Protection library. I'm also replacing spaces with underscores because I read that IE doesn't like spaces in window names. FYI the original string was "New Window: Jamie" and it looks like the ":" gets encoded as "\x3a". The window opens in FireFox just fine, but the window does not open in IE8. Does IE8 just not like this encoding, or the character or what? Are there rules around what characters can appear in the window name for IE8?


Solution

  • Are there rules around what characters can appear in the window name for IE8?

    Yes. Although it doesn't seem to be documented, IE has always required that a window name be composed of alphanumerics and underscore. A colon won't be accepted, whether read from an encoded string literal or not.

    If you really needed to map an arbitrary string to a unique name-safe version you'd have to do something like encoding every non-alphanumeric character into an escape sequence, eg:

    function encodeToName(s) {
        return s.replace(/[^A-Za-z0-9]/g, function(match) {
            var c= match[0].charCodeAt(0).toString(16);
            return '_'+(new Array(5-c.length).join('0'))+c;
        });
    }
    
    alert(encodeToName('New Window: Jamie'));
    // 'New_0020Window_003A_0020Jamie'
    

    I agree with casablanca though, it seems very unlikely you should actually need to do this. The user is never going to get to see the window name, so w1 is just as good. It's rare enough that you need window names at all.