Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get URL parameter using jQuery or plain JavaScript?

I have seen lots of jQuery examples where parameter size and name are unknown.

My URL is only going to ever have 1 string:

http://example.com?sent=yes 

I just want to detect:

  1. Does sent exist?
  2. Is it equal to "yes"?
like image 429
LeBlaireau Avatar asked Oct 21 '13 09:10

LeBlaireau


People also ask

Can you use Javascript to get URL parameter values?

The short answer is yes Javascript can parse URL parameter values. You can do this by leveraging URL Parameters to: Pass values from one page to another using the Javascript Get Method. Pass custom values to Google Analytics using the Google Tag Manager URL Variable which works the same as using a Javascript function.

How can I get URL in jQuery?

The current URL in jQuery can be obtained by using the 'href' property of the Location object which contains information about the current URL. The 'href' property returns a string with the full URL of the current page.


1 Answers

Best solution here.

var getUrlParameter = function getUrlParameter(sParam) {     var sPageURL = window.location.search.substring(1),         sURLVariables = sPageURL.split('&'),         sParameterName,         i;      for (i = 0; i < sURLVariables.length; i++) {         sParameterName = sURLVariables[i].split('=');          if (sParameterName[0] === sParam) {             return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);         }     }     return false; }; 

And this is how you can use this function assuming the URL is,
http://dummy.com/?technology=jquery&blog=jquerybyexample.

var tech = getUrlParameter('technology'); var blog = getUrlParameter('blog'); 
like image 117
Sameer Kazi Avatar answered Oct 07 '22 04:10

Sameer Kazi