简体   繁体   中英

How to get parameter from hash url?

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!

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.

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].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM