[thelist] Prevent a request from redirecting browser....

Juha Suni juha at ilmiantajat.fi
Wed Mar 30 02:30:11 CST 2005


Jay Blanchard wrote:
> Good morning!
>
> Here is the scenario. You have created an application that will be
> used in-house. It is opened in a browser window where, via
> JavaScript, you have eliminated all but the title bar.
>
> If it is the only browser window open and you click a link in an
> e-mail you have received that link proceeds to load in your
> application window, which is not a good thing because you might lose
> application data since you have the back button disabled (as much as
> it can be).
>
> Here is the question. Does anyone know of a way to prevent the site
> from opening in the browser window or having the newly clicked site
> open in a new browser window? I have in my mind something like a
> JavaScript interception.....

This problem sounded interesting, since I've been creating a lot of web apps
lately, most usually dealing with multiple windows and possible popups, and
I have periodically stumbled upon this same problem. I never really had the
energy to tackle it though, but I decided to give it a try now. It was
surprisingly easy. (scroll to bottom for solution, first a brief
explanation).

One thing I have learned that when opening links in windows, it has most to
do with the window name. If a window with the specified name (target
attribute of the anchor element) already exists, the link opens up there,
otherwise a new window is created.

So I decided to do a little test. I made a simple page the upon loading
shows the window name with javascript (<body onload="alert(self.name);">).
Using the most popular stuff (IE 6/Win, Outlook Express) I found out that
loading the page normally (without an email link) resulted in the window
having no name at all. If I opened the same page from a link within Outlook
Express, the window had the name "_unspecifiedFrame".

Thus, if I had no IE windows open, or if all the open windows had been
opened by clicking the IE icon (i.e. not with an email link), the email link
would open up a new window. The next email link would however use this same
window, since it already had the name "_unspecifiedFrame".

So how do you fix this? Simple. Just make sure you web app always
automatically sets its name spcifically to something of your own taste.
After this only links within that window, or exteral links with that window
set specifically as target, use that window. So add the short piece of
javascript to the head-element of the pages in your app:

<script language="JavaScript" type="text/JavaScript">
<!--
self.name='myappwindow';
//-->
</script>

Alternatively, to avoid using javascript, when you open the actual popup
(like in Jay's case), use the target attribute to define the window name.

Since I still had only limited time, I did not test out with other email
clients and browsers. I still am rather certain that all email clients set
some kind of default target attribute to links they create, and it would
certainly be weird if even ignoring that, they had no respect whatsoever on
the existing windows and their names when deciding whether to spawn a new
window or not.

To test it out, try clicking the following link several times. If you have
javascript enabled, it should spawn a new window every time (given enough
time for the previous to finish loading, which is not much).

http://www.ilmiantajat.net/testing/zuni/

Hope that helps, and please let me know of any problems. I'd be happy to
implement this completely to my existing applications.

-- 
Suni



More information about the thelist mailing list