mapping of url parameters


/ Published in: JavaScript
Save to your folder(s)

given a url:
>`http://www.someurl.com/page.html?param1=foo&param2=bar`

You can use map.param1 outside of the function such as:
>`$('#some_hidden_field').val(map.param1);`


Copy this code and paste it in your HTML
  1. var map = {};
  2. function getUrlVars() {
  3. var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
  4. map[key] = value;
  5. });
  6. return map;
  7. }
  8.  
  9. getUrlVars();
  10.  
  11. console.log(map.param1);
  12. console.log(map.param2);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.