3

I'm opening a window like this:

var w = window.open("", "window-name", opts);

Then I create an URL using some dynamic data and set the window to that URL like this:

w.location.href = url;

My problem is that I need to retrieve the URL I previously set to the window later for editing. Doing this doesn't work:

var url = w.location.href;

Because that's a security violation in my case and triggers an exception.

I can't just save the URL as a "global" variable since this needs to work across pages. I don't want to use cookies, because the URL should be reset when the window closes. Using cookies would add more complexity since it forces me to do cleanup before the window closes, and it adds bug risks. I will use it as a last resort.

Is there any way to retrieve the URL of a window instance after the URL has been set to something on a different domain?

Manquer
  • 7,390
  • 8
  • 42
  • 69
Hubro
  • 56,214
  • 69
  • 228
  • 381
  • You don't need to cleanup the cookie when window closes if you don't set an expiration. This will set the cookie to expire at end of browser session, so the browser cleans it up for you. In any case, the only other thing I can think of for you to do is use a server-side session var to persist the url from page to page (e.g. output as a js var). – CrayonViolent Jun 03 '14 at 16:18
  • Alternatively you could pass the url as a url=[url] param to each page. But that could get pretty messy pretty quick too. Or you could make use of [`sessionStorage`](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#sessionStorage) though it's html5 so maybe not compatible enough for you – CrayonViolent Jun 03 '14 at 16:23
  • @CrayonViolent: The cookie would have to be cleared every time the pop-up window is closed. The data I gather should only persist as long as the pop-up window is open, which is why saving the data **on the window reference** would have been perfect, if not for the security violation. – Hubro Jun 03 '14 at 17:19

2 Answers2

0

Use the window.name property like this...

var w = window.open('http://www.example.com', 'www.previousurlhere.com', opts);

Then, when new window opens (assuming it's yours), from within the new window you can grab the window.name data like...

var previousUrl = window.name;

And that should do it.

Jason
  • 591
  • 2
  • 10
  • 23
  • But then how am I supposed to retrieve the window reference from another browser page? I can't have a dynamically generated window name, it has to be something static like "my-popup". – Hubro Jun 03 '14 at 17:15
  • I'm afraid I may be having trouble envisioning your objective. It seems like you're asking to determine, from a popup, "Who is my maker?" so my suggestion was to put the maker's name (URL) into the `name` property of the `window.open` function. Doing this means that every popup is created aware of its maker's name. Is this not the goal? – Jason Jun 03 '14 at 17:23
  • No I'm not trying to determine the maker of the popup, I'm just trying to fetch current URL of the popup window. Using the name property of the window to hold the URL is not an alternative, as the name has to be a constant value. – Hubro Jun 03 '14 at 21:15
-1

The browser won't tell you the state of previously visited URLs of other domains. That happens for security reasons.

If you're working on the same domain, perhaps you can query the History API to get what you need. Alternatively, you may store the visited URLs on the client via LocalStorage or another solution.

Razvan Caliman
  • 4,509
  • 3
  • 21
  • 24