Forum Moderators: open

Message Too Old, No Replies

Creating window popups, an identifying problem with popup blockers

Popups blockers are screwing with this popup process

         

Seige

11:59 am on May 5, 2005 (gmt 0)

10+ Year Member



I made a simply piece of javascript function to call out popups. However, different users seems to experience different problems, some claims they couldn't view the popup at all despite not having a popup blocker.

function openpopup(htmlcode, url, width, height, scroll, resize) {
if (newwindow) newwindow.close();
properties = 'width=' +width+ ',height=' +height+ ',scrollbars=' +scroll+ ',resizable=' +resize;
var newwindow = window.open(url,'newwindow',properties);
if (newwindow) {
if (!newwindow.opener) newwindow.opener=self;
if (htmlcode!="") newwindow.document.write(htmlcode);
newwindow.focus();
} else {
alert("Process interrupted and may have been incomplete.\n\nPlease disable your popup blocker!");
}
}

This popup function identifies popup blockers, and if the window did not open, it'll display an alert.
It's also capable of writing HTML into the new popup window.

Is there any expert here that have a better solution? Or able to see any fault in this function such that some users are unable to call the function?

Bernard Marx

1:30 pm on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if (newwindow) newwindow.close();

This line must be wrong. It tests for a true value of a global variable, newwindow.
Firstly, unless this variable has been previously declared, you'll get an error.
Secondly, the function sets a local variable, newwindow, not a global, so the test will always be false, even the if the global variable has been declared.

Seige

2:38 pm on May 6, 2005 (gmt 0)

10+ Year Member



I see what you mean now. So, how do you set a global variable for window.open?

var newwindow = new Function; // is this right?

This line is used to see if there is another new window opened, and if there is, it'll close it before opening a new window.

If I can't use this line, what can I do?

Bernard Marx

3:09 pm on May 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var newwindow = new Function; // is this right?

? Going off on a tangent there.

1. Declare the variable outside the function:

var newwindow; // now if(newwindow) won't cause an error

2. Inside the function itself, when you come to set the variable, don't use the keyword, var (else you will be setting a local variable, not assigning the object ref to the global).

As to the whole process working in its entirety...I have no idea!

Seige

12:34 am on May 7, 2005 (gmt 0)

10+ Year Member



I understand the relationship between local and global variable. But I'm not sure what the responce for this:

if(newwindow) //will newwindow return true or false?

I guess my question should be, how to determine if a window which has been assigned a name is opened. Just so that I can close it.

(i wonder why i'm not receiving email notifications of this thread? I have madesure to tick it everytime I post)

Seige

1:26 pm on May 7, 2005 (gmt 0)

10+ Year Member



This is the new set of codes, a few lines have been disabled:

var newwindow;
function openpopup(htmlcode, url, width, height, scroll, resize) {
if (newwindow) newwindow.close();
properties = 'width=' +width+ ',height=' +height+ ',scrollbars=' +scroll+ ',resizable=' +resize;
newwindow = window.open(url,'newwindow',properties);
//if (newwindow) {
if (!newwindow.opener) newwindow.opener=self;
if (htmlcode!="") newwindow.document.write(htmlcode);
newwindow.focus();
//} else {
//alert("Process interrupted and may have been incomplete.\n\nPlease disable your popup blocker!");
//}
}


I fixed an error handler to send me all error messages. I get mysterious error messages from different computer around the world.

Error message= Object is not connected to server
Error message= 'document' is null or not an object
Error message= Unspecified error.
Error message= The callee (server [not server application]) is not available
and disappeared; all connections are invalid. The call did not execute.

Among these lines, can't really tell which lines since different browser interpret line numbers differently.
if (!newwindow.opener) newwindow.opener=self;
if (htmlcode!="") newwindow.document.write(htmlcode);
newwindow.focus();