Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript "window.open" code won't work in Internet Explorer 7 or 8

I am using this chunk of jQuery/Javascript code on my site to open a popup window:

$('#change_photo_link').click(function(){
    $id = $('#id').attr('value');

    window.open("photo.upload.php?id=" + $id,"Upload Photo",
    "menubar=no,width=430,height=100,toolbar=no");
});

This code works on Firefox and Chrome. It does not work on IE7 or IE8 (haven't tested IE6). IE pops up an error on the line window.open. Why? The error that IE gives is "Invalid Argument" and that's all.

like image 737
James P. Wright Avatar asked Feb 03 '10 03:02

James P. Wright


People also ask

How do you open a new window with JavaScript?

You can use JavaScript to launch a new window. The window. open() method, which allows you to open up new browser window without navigating away from the current page. It is useful when you need to display some popup advertisement or the instructions without navigating away from the current window.

What is window open JavaScript?

open() The open() method of the Window interface loads a specified resource into a new or existing browsing context (that is, a tab, a window, or an iframe) under a specified name.


2 Answers

It's the space in the second parameter that's causing it. If you use "UploadPhoto" instead of "Upload Photo", it works:

$('#change_photo_link').click(function(){
    $id = $('#id').attr('value');

    window.open("photo.upload.php?id=" + $id,"UploadPhoto",
    "menubar=no,width=430,height=100,toolbar=no");
});

I can't seem to find any official reasons as to why having a space in the windowName parameter of window.open() causes an error, but it's likely just an implementation detail. The windowName is used as an internal reference, and can be used as a value for a target attribute of an anchor or form, so I guess IE can't handle that internally. The reference docs for Gecko/Firefox says that this parameter should not contain spaces, but clearly current versions of Gecko don't have a problem with it if it does.

like image 147
zombat Avatar answered Nov 11 '22 15:11

zombat


The windowName argument can only contain alphanumeric characters and underscores (i.e. [A-Z0-9_]).

You must change

window.open("photo.upload.php?id=" + $id,"Upload Photo",
"menubar=no,width=430,height=100,toolbar=no");

to

window.open("photo.upload.php?id=" + $id,"Upload_Photo",
"menubar=no,width=430,height=100,toolbar=no");

or some other name that doesn't have spaces.

See https://developer.mozilla.org/En/DOM/Window.open.

like image 38
ntownsend Avatar answered Nov 11 '22 15:11

ntownsend