Reza
Reza

Reputation: 684

jQuery ajax navigation with ? instead of #

So here is what I need to do.

I have a web app that needs to work with these kind of URLs:

http://app.com/?this_is_similar_to_anchor

By default, I can use window.location.hash and just use the # to change things on the URL. But I noticed a few websites are using a question mark instead of hash. And this is what I need.

Why do I need it?

I need to send whatever after # comes to the server, but CAN NOT send it using ajax once the page loads.

So, its vital for the app to have whatever comes after # in order to process the page.

That's when I came to the point of using ? instead, so the server side also has access to it.

Its like client side and server side are sharing this parameter.

I tried different methods to just use:

window.location.href= 'add_parameter';
event.PreventDefault(); or return false; etc to stop the browser to actually change the URL.

TLDR: I need to be able to change the URL from this:

www.site.com/?url1

to this

www.site.com/?url2

on the fly, without reloading the page and NOT using #. (Or any way to convert the # to ?)

Thanks,

Upvotes: 0

Views: 95

Answers (2)

Quentin
Quentin

Reputation: 943936

Use pushState which is what Github do, but note limited browser support.

Upvotes: 1

arik
arik

Reputation: 29320

Take a look at this:

history.pushState(stateObj, "page 2", "bar.html");

See Manipulating Browser Histroy.

However, this will NOT send information to the browser, because it will NOT make the page reload. If however you don't want the page to reload, I'm afraid you'll either have to use AJAX, or, if you can't, maybe submit a form to a hidden iframe.

Upvotes: 1

Related Questions