Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using slash in window.location.hash

If I change the hash like so: window.location.hash = "main/0/sub/1/na/false";. The address in the browser changes to http://mysite.com/#main/0/sub/1/na/false. Page's onhashchange function fires and everything works like it is supposed to.

However, in Firebug I can see that I'm also sending a request to: http://mysite.com/main/0/sub/1/na/false ... URL without hash, which results in a silent 404 in the console.

When I debug I find that it happens at the window.location.hash point.

But, if I change the hash like so: window.location.hash = "main=0&sub=1&na=false"; no additional request is sent.

Why is the additional request being sent in the first example?

UPDATE: I noticed it sends the request after window.location.hash and before (during?) $(window).bind('hashchange'). Example if I have ...

window.location.hash = 'main/0/sub/1/na/false'; // Breakpoint 1 in Firebug

$(window).bind('hashchange', function(e) {
    e.preventDefault();   // Breakpoint 2 in Firebug
    e.stopPropagation();
});

When it stops at breakpoint 1, no request is sent. When it stops at breakpoint 2, the request is already sent.

I can see in Apache Tomcat that the request is being sent, too.

I'll add that I have jQuery and jQuery Mobile plugged in.

UPDATE 2: Removing jQuery Mobile resolves the problem. However, I need it :/

UPDATE 3

If anyone is interested: With jQuery Mobile: http://jsfiddle.net/pioSko/hz5PU/3/

Without jQuery Mobile: http://jsfiddle.net/pioSko/hz5PU/4/

Open Firebug or other debugging app and test the links.

like image 596
pioSko Avatar asked Jun 27 '12 11:06

pioSko


1 Answers

I had a similar problem when using History.js. I think that's intended behavior for that script, as it's designed to make URLs pretty (non-hashy) while also not reloading the page.

like image 112
iameli Avatar answered Oct 20 '22 18:10

iameli