Search This Blog

Javascript win.open() not working in IE

posted on Sunday, June 10, 2012


Before, I didn't really like to use Javascript, but i've been using it more and more lately and I'm really starting to see the benefits. The syntax remains quite 'unnatural' to me, but even that seems to be improving! So, with a little help of JSFiddle and JSLint, one day, I might even get the hang of it :P

So, back to the post! When I was recently working my Javascript-awesomeness, I used win.open(), which worked great in Firefox and Chrome but did absolutely nothing in IE...

There are two possible solutions that immediately come to mind:
  • The pop-up is actually working, but a pop-up blocker is blocking it.
  • The pop-up isn't working because as far as IE is concerned your syntax is wrong.
In the first case, you can just 'unblock' the pop-up and that's it. In the second case, you need to change the syntax. I did some research and found that IE can be quite picky about the second parameter of the open-method, the name of the window. In IE, the window name can not contain spaces, hyphens or apostropes. Underscores are probably the best alternative. You could also leave this parameter empty. As far as I know, this problem occures in IE7, IE8 and even IE9.

Syntax

window.open(URL,name,specs,replace)

For example

The code below won't work in IE, since this contains spaces and an exclamation point:
window.open( 'https://www.google.com', 'This will open Google!', 'width=300,height=200,left=600,top=400,scrollbars=no,copyhistory=no,toolbar=no,location=no,menubar=no,directories=no,status=no,resizable=yes' );

The code below should work:
window.open( 'https://www.google.com', 'Google', 'width=300,height=200,left=600,top=400,scrollbars=no,copyhistory=no,toolbar=no,location=no,menubar=no,directories=no,status=no,resizable=yes' );

You can try this out in different browsers using the following fiddle: https://jsfiddle.net/4bbhu/2/ (link intentionally broken as this website seems to no longer exist). Chrome and Firefox should show two pop-ups, IE should show only one.

Below you can find some resources about the Javascript window.open()-method, listing all possible parameters:

W3Schools - https://www.w3schools.com/jsref/met_win_open.asp
Javascript-coder - https://www.javascript-coder.com/window-popup/javascript-window-open.phtml

Could be useful, right?


No comments:

Post a Comment