How to get parameter from hash url?

Go To StackoverFlow.com

0

On some of my pages, I have a hash in the url like this: http://localhost/#/products/6959

How can I check if I have a product ID available and so I can save it in a variable to use later? Any help would be greatly appreciated!

2012-04-05 21:26
by TruMan1
Could you elaborate on "How can I check if I have a session ID I can use?" By "Can use", do you mean "Hashtag contains a session ID"? You also mentioned validating session ID - While we can do a lot of validation in terms of session ID's format, the only reliable way to validate it on the server-side. FYI - DashK 2012-04-05 21:31
so you want to grab the number on the end - Matt K 2012-04-05 21:32
It's called a fragment, by the way - Joey 2012-04-05 21:40
I realize the wording was a bit confusing, so I was hoping just to get the product ID from the hash exampl - TruMan1 2012-04-05 21:40


0

You want a regexp to extract the number at the end of the url in javascript?

"http://localhost/#/products/6959".match(/[0-9]+$/);

Edit: Or if you want to make sure this is products:

"http://localhost/#/products/6959".match(/products\/([0-9]+$)/)[1]);

The parenthesis indicate a matching group that you find in [1].

2012-04-05 21:45
by barsju
Cool, but how do I also make it check that products in the hash? Sometimes I will have this url: http://localhost/#/categories/695 - TruMan1 2012-04-05 21:51
Updated the answe - barsju 2012-04-05 21:55


0

You should be able to get that id out of the URL through accessing the params variable. In this case, params[:id] should do the trick.

Also, I do not recommend using a regex.

2012-04-05 21:41
by Josh Moore
That sounds interesting. Is this built into Javascript or do I have to use a 3rd party library - TruMan1 2012-04-05 21:47
This is the Rails convention of interacting with the url string. Different languages and frameworks allow you access this information in various ways. For instance in PHP, you could get this data from the $_REQUEST variable - Josh Moore 2012-04-05 22:06
Ads