IE window.open and no referrer
In Internet Explorer, when you call the method window.open in javascript it does not pass the referring url to the new window that is created. This happens because the request to the page in the new window does not contain the HTTP Referer header. Therefore if you try to access the document.referrer variable in javascript or the $_SERVER['HTTP_REFERER'] variable in PHP from the new window they both will not be set. In FireFox it works fine, and the referring url is passed to the new window in the HTTP headers. But since most of the people in the world use Internet Explorer a workaround must be put in place.
Instead of doing the following
<a href="javascript:window.open('newurl.php','','');">
click here</a>
We can pretty much simulate the same effect with a simple target=”_blank”
<a href="newurl.php" target="_blank">click here</a>
Furthermore, if you are only planning on accessing the referring url in javascript and not in PHP you can access the window.opener object and you can still mantain the use of window.open.
var ReferringUrl = ""; if (document.referrer) ReferringUrl = document.referrer.toString(); if (window.opener && window.opener.location) ReferringUrl = window.opener.location.toString();
That script will allow you to retrieve the referring url in the new window through javascript but it will still be unaccessible through PHP.
So to allow tracking of the referring url using window.open so that it is available in javascript and PHP we can implement the following, where we actually send the referring url and other such data to through the url in the query string.
<a href="javascript:window.open('newurl.php?' + urlencode
(document.location.toString()),'','height=200,width=150');">
click here</a>
That script will allow you to maintain the use of window.open and still be able to get the referring url in both javascript through the parsing of document.location in the new popup window and through PHP using the $_REQUEST['referringurl'] variable. Note that urlencode is not a native javascript function.








August 18th, 2006 at 1:40 pm
If you use a combination of window.open and specifying a target, you can still specify attributes for the new window (like height, width, menus, etc):
click here
August 18th, 2006 at 1:43 pm
If you use a combination of window.open and specifying a target, you can still specify attributes for the new window (like height, width, menus, etc):
<a href="newurl.php"
onclick="window.open(”,’newwin’,'height=200,width=300′)"
target="newwin">click here</a>
September 6th, 2006 at 6:58 am
Actually, I tried it and Kevin’s suggestion does work. Window.open() creates the new window with a blank URL (and all the attributes you want), and then the target=”newwin” tells that new window to open your desired URL. Since the URL was sent via href= and target=, the referer is intact in the new window.
Does anyone know if this is a new problem with IE? We’re evaluating some open source software that doesn’t work in IE because of this issue, and I’m curious if this is a new thing.
September 6th, 2006 at 11:51 am
Interesting..
So as long as there is a target specified then it will copy the referring url. I would assume the problem has been there all along.
October 23rd, 2006 at 9:12 am
Kevin sugestions works on IE and FF
u can also set the $_SESSION['referer']=$_SERVER['REQUEST_URI'] as a session var on the 1st page and then use it on the second page.
November 10th, 2006 at 3:21 pm
Stumbled upon this through a Google search. Solved my problem nicely. Thanks Nathan and Kevin.
November 15th, 2006 at 11:36 am
Like Jorsh said, ran accross this via a google search and it saved me quite some bit of headache this morning. Thanks guys!
March 29th, 2007 at 10:41 am
I tried Kevin’s suggestion and I still cannot get the new window to resize. It contines to open as a default blank. Below is the code I used. Any suggestions would be much appreciated. The browser used was IE7.
Email This Page
Thank you,
Alex
September 26th, 2007 at 5:20 am
Alex, have you copied the code from this page?
If yes, try replacing the typographic quotes for the window.open parameters by normal single quotes.
This worked for me.
February 25th, 2008 at 12:06 am
Great!!! It solved my problem. Thanks Kevin.
June 18th, 2008 at 8:54 am
Hi, Kevin Clark suggestion work fine, but anyone know the way for do the same with a swf file??
Thanks guys
July 23rd, 2008 at 2:07 am
what if the referrer is not my website and it uses document.location to redirect to my website? any ideea how can i get the referrer in IE for this case?
August 4th, 2008 at 7:27 pm
This subject is exactly my problem, and Kevin’s solution works on my site.
However, I am a designer that understands coding for “cut and paste” application - but am basically out of my element if I have to modify scripts to any large degree.
Thus, my problem… getting Kevin’s solution to work with the login script that I am using on my site:
Login
Area
UserID:
Password:
Kevin’s solution that works on my page:
click here
Any assistance combining these would be greatly appreciated.
Just a note on the slight change to Kevin’s code:
with onclick, I ended up with a basic “self contained” popup window - while onleftclick gave me a standard resizable browser window with access to all the browser functions (back, forward, etc)… don’t know why it makes a difference, but it does.