Click to See Complete Forum and Search --> : trying to open new window


kennmurrah
11-19-2003, 01:59 PM
Greetings.

've been staring at this line of code for too long without being able to see why it's behaving the way it is ....

I'm trying to open a new window on top of the current window, and it does that, but the "original" (bottom) window changes to read "Object Window" ... I can get back to the original with the "Back" button but of course don't want to ask that of the user ... I simply want a new window to appear on top of the original, after which the user can close the top window and be back where he started ....

what the heck am I doing wrong?

<a href='javascript:window.open("3.jpg","","height=400,width=300,left=80,top=80,scrollbars=1")'>this is a test </>

Thanks VERY much for any help.

kenn

fredmv
11-19-2003, 02:12 PM
Welcome to the forums.

I'll explain what's wrong. Realize that you're using the JavaScript pseudo-protocol in the href attribute of your <a> tag. When you include JavaScript code after the JavaScript pseudo protocol, it will print whatever the last return value was. So, when you open a new window, it is sending back the return value [object Window] as a result of the new window opening. To prevent this, you must use the void operator to prevent the last return value from being printed, which as a result, overwrites the entire document. You could do this, for example:

<a href="javascript:window.open('3.jpg', '', 'height=400,width=300,left=80,top=80,scrollbars=1'); void 0;">Open window.</a>

This will work exactly as expected. The window will open and now, no return values will be printed and overwrite the current document because of the void operator that supresses the return values. However, while this method may work, it still isn't a good one to use and I'll explain why.

By using this method, the link doesn't become degradable. This means that for users that have JavaScript turned off in their browsers or have browsers that simply don't support JavaScript, they won't be able to use this link. The link will do absolutely nothing for them since it requires the use of JavaScript, hence the JavaScript pseudo-protocol.

The best way to create a link while still being degradable is to define the link like you normally would using the <a> tag's href attribute, then in your onclick event handler simply pass the current value of the link to the window.open method, and finally, returning a false boolean value so the event is cancled and the user isn't brought to the URL specific in the href attribute. Then, for users with JavaScript, it will open a new window, and for users without JavaScript, it will act as a normal link. Here's an example of how you could do this:

<a href="3.jpg" onclick="window.open(this.href, '', 'height=400,width=300,left=80,top=80,scrollbars=1'); return false;">Open window.</a>

I would highly recommend using this method because your content would then become accessible to more users, which is really what you want to be doing. I would also like to note that opening new windows can be a bad idea as it destroys accessibility (http://www.diveintoaccessibility.org/day_16_not_opening_new_windows.html), but I suppose if you know what the audience of your site is going to be, this may or may not be a problem.

I hope that helps you out.

kennmurrah
11-19-2003, 02:45 PM
THanks for the TERRIFIC reply .... It worked exactly as you said it would, and I'm making plans to redo much of my code on this project to incorporate your suggestions.

Thanks again.

kennM

fredmv
11-19-2003, 02:47 PM
You're very welcome. :D