user2310209
user2310209

Reputation: 1002

How to get url params value and pass it in url using jQuery

I having url in query string like http://www.domain.com:3000/allot_room?floor=6&hostel=1&student=1.In that, how to get student parameter value using jquery.

I have used this code

var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

It shows like floor=6,hostel=1,student=1.In that how to get student parameter value.And I tried different codes.

Upvotes: 0

Views: 168

Answers (2)

Mohsen
Mohsen

Reputation: 269

try

window.location.search

see mozilla documentation

Upvotes: 2

Moazzam Khan
Moazzam Khan

Reputation: 3170

use var url = window.location.search

and from MDN -

Get the value of a single window.location.search key:

function loadPageVar (sVar) {
  return decodeURI(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURI(sVar).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}

alert(loadPageVar("floor"));
alert(loadPageVar("student"));

More about window.location

Upvotes: 2

Related Questions