Skip to content Skip to sidebar Skip to footer

Pass Url Parameter Without Question Mark And Equal Sign?

I'm using javascript to retrieve a url parameter from the browser and pass it to a server running sockets.io. It's working exactly as intended, but I would like to grab the paramet

Solution 1:

Certainly possible to grab the url.

window.location.href returns the full path of the url. You can split it using .split('/')

So windows.location.href.split('/') will give you an array of strings where each element is a different part of the url. For example running it on example.com/page/var1/var2 would give you an array ["example.com", "page", "var1", "var2"].

Keep in mind that your server is still going to need to know how to rout requests to example.com/page/var1/var2. So if var1 and var2 are variables for example.com/page, your sever somehow needs to know to serve example.com/page.

Solution 2:

var urlParam = location.search.split("var=")[1];

var mapObj = {
   "?":"",
   "=":"-"
};

urlParam = urlParam.replace(/(\?|=)/g, function (matched) {
  return mapObj[matched];
});

Post a Comment for "Pass Url Parameter Without Question Mark And Equal Sign?"