views:

1193

answers:

5

Hello everyone.. this is my first question.. so, here we go.

i have a site, 100% xhtml/css with some ajax functions thanks to jquery.

te problem is.. for all the "sub pages".. the url remains the same (index.php)..

my question is..

does jquery allow some url parameters to trigger an specific function?

example: www.mypage.com -> home page.. if i click on.. let's say products.. the url will be the same.. and if i try to send the url to a friend (copy & paste) it will be www.mypage.com.. so.. if my friend clicks on the link he will be redirected to the home page instead of the products page wich is the section that i want to share with him.

is there anyway to set the url like.. www.mypage.com?s=products to trigger the jquery event without using php?

thanks in advance.

+2  A: 

The jquery querystring plugin provides an easy way to parse query strings.

Bayard Randel
+2  A: 

It sounds to me like you want to change the querystring without reloading the page. This is impossible. What you can do is change the #anchor part of the url, so your ajax links would set the location to mysite.com/#products and then use jquery to detect at load time and act accordingly.

Sam
+2  A: 

Usually fake pages in an AJAX application change the querystring through adding something after a #, like www.mypage.com/#products. If you try to change the URL to a different URL in JavaScript, the browser will redirect to that page. However, if the change is after a #, it will stay on the page (and try to put an element with that ID at the top of the browser window).

So, you could try something like the following JavaScript to set the URL:

window.location.hash = 'products';

Then you can look for that when you load the page, so that you know which page to retrieve.

There are also a number of ready-made JavaScript libraries that take care of making history work correctly in these sorts of scenarios, though I don't know enough to recommend a particular library.

bdukes
+2  A: 

http://blog.rebeccamurphey.com/2007/12/04/anchor-based-url-navigation-with-jquery/

Never done this, but this site has pretty straight forward method.

Basically you specify an anchor tag for each logical page you have and update the url accordingly when a user navigates to that logical page.

When a new user arrives, you parse the query string (as mentioned above) and then execute the appropriate navigate action.

eyston
this is exactly what i was looking for, it solved my problem... thanks for the link! :)
Andy
+1  A: 

There are a lot of plugin available for this kind of functionality. The keyword to finding them is "history"

Paolo Bergantino