views:

71

answers:

2

Hello there,

I currently have a issue with firefox, where all other browser behave in the right way - even IE6!

What I want to do is redirection to a subpage but leaving a history entry. There are 2 methods of rewriting the url as far as I know:

  • window.location = "some.url"; - redirect to some.url with history entry
  • window.location.replace("some.url"); - redirect without history entry

So I have to use the first one and tested in the firebug console everthing works fine.

Now there is the kind of strange part of this question: the same statement, that worked fine in the console doesn't in some jQuery callback handler:

jQuery("#selector").bind("submit", function() {
  $.getJSON("some_cool_json", function(response) {
    var redirect_path = response.path;
    window.location = redirect_path;
  });
  return false;
});

where response_path is set correctly, I checked it! Even the redirection is working correctly, but there is no history entry created.

Any ideas on that one? Would be great! ;)

Cheers

Joe

+5  A: 

hi,

use assign():

window.location.assign("http://...");

replace(url)
Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

henchman
Hi Henchman and thanks for your answer, I forgot to mention I tried `assign` but it didn't work as well!
Joe
+1 `assign()` is more correct than `window.location = "http://..."`, although this doesn't explain why what's happening is happening.
Andy E
perhaps firefox is using .replace() when using window.location = ""?!
henchman
The workaround of Pointy is working, but I'll update my code to use `assign` - its way nicer than the = assignment! ;)
Joe
+2  A: 

If this were happening to me, a thing that I'd try would be this:

jQuery("#selector").bind("submit", function() {
  $.getJSON("some_cool_json", function(response) {
    var redirect_path = response.path;
    setTimeout(function() {
      window.location.assign(redirect_path);
    }, 1);
  });
  return false;
});

The idea is to put the execution of the "assign()" call into a "normal" event handler, in case there's something about the context of the "getJSON" response function that's weird. That function (the "getJSON" response) will be called from the context of the browser executing the code of a <script> block that's just been added to the DOM, so it's at least a little bit unusual.

I don't know that it'll work; I haven't tried setting up a test page.

Pointy
Sounds strange, but thats it! Thank you! Would be interesting to have a jQuery guru in the line to understand what is happening on the internals! ;)
Joe
What jQuery does is this: it builds a `<script>` tag with your URL as the source, after adding a parameter to tell the JSON server what function to call. The server returns a block of Javascript that looks like "functionName(/* ... json block ... */)". Now when the browser loads the script, the script runs, and the function is called. jQuery builds that function for you, and all it does is call the callback you pass to getJSON.
Pointy