views:

318

answers:

6

I know how to use window.location. The problem is that, (at least in FF) it erases the page you are on from the history. e.g., I'm on page A, I navigate to B and window.location is used to send me to C. If on page C, I click back, I end up at A, not B.

Is there some other way to navigate to another page without erasing the current page from the history?

EDIT: This wont happen if you run the code in Firebug.

Here is a super simple page (yes I know the code is ugly, it's not the real code) that shows the problem:

<html>
<head><script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"&gt; </script>
<body>  
<div class="foo"></div>
<a href="javascript:$('.foo').html('<scri'+'pt type=&quot;text/javascript&quot;>window.location = &quot;c.html&quot;</s'+'cript>');">b</a>

EDIT2: jQuery is the problem and setTimeout is the answer. I've added my solution below just in case someone else runs into this weird edge case.

EDIT3: This example is simplified to make it easier to test. In the real case, the click handler makes an Ajax call that returns the HTML, so we can't simplify the whole thing by just writing window.location = whatever. The code comes from the server, embedded in the HTML.

A: 

Strange. I guess you could use a hyperlink, then trigger click().

Matt
A: 

Use location.href instead of location.

Robusto
A: 

You could use window.location.assign(URL).

Satyajit
Nope, same result.
noah
A: 

I'm not seeing that behavior in Firefox 3.6. Following your example, from stackoverflow.com I went to google.com, then from there I used the Firebug console window to set window.location = 'http://facebook.com'. Once there, all three were in my browser history..

m4olivei
I agree, even on this site only.. click another link, then in firebug console, type "location = '/';" and then press back (takes you to the second page). Compare that to the same actions, but typing "location.replace('/');" (takes you to this page).
gregmac
Yea, Firebug doesn't trigger it.
noah
A: 

This is just a hack and I don't know if it will work. Try window.history.back(1) after you have set the URL using window.location = URL;

Hope this helps.

Thanks,

Raja

Raja
A: 

setTimeout is the answer.

$(div).html('<script...> setTimeout(function() { window.location = url; },10); </script>');

Doesn't have this problem. It must have something to do with the way jQuery executes inline scripts (by creating a script tag in the head element).

noah
Do you have to have to be inserting dynamic script tags from the `href` of an `A` tag like that? I wonder if there is a different, less complicated way that you could do this that would also avoid the problem.
Renesis
??? Why are you using javascript, to insert html, which has a script tag containing javascript, which runs a timer to run the real javascript code? Why don't you replace the whole thing with just "window.location = url;" ? YES, you CAN turn left 3 times to make a right... but you can also just turn right.
gregmac
Yes actually. The use case is dynamically loading HTML content, but if the load fails, we need to redirect, and the logic for determining where to redirect to is on the server so this is actually the simplest way to accomplish it.
noah