Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get first argument from url in javascript

Tags:

javascript

url


I just wanted to find my first argument from my url.

like

http://www.google.co.in/webhp?ix=teb&sourceid=chrome&ie=UTF-8

I just wanted to string "ix=teb".

How to get that string only.

like image 245
Nitz Avatar asked Oct 20 '25 15:10

Nitz


2 Answers

window.location.search.replace("?", "").split("&")[0];

update:

in case there's a variable holding url:

url.substring(url.lastIndexOf("?") + 1).split("&")[0];
like image 147
Kamyar Nazeri Avatar answered Oct 22 '25 04:10

Kamyar Nazeri


You can use a regular expression to read out parts of the URL:

window.location.search.match(/\?([^&$]+)/)[1] // == 'ix=teb'

In the event that there isn't anything in the query string of the URL, this would cause an error, so you should include some type checking like:

var queryStringMatch = window.location.search.match(/\?([^&$]+)/);
if(queryStringMatch) {
   // do something with queryStringMatch[1]
}
like image 34
steveukx Avatar answered Oct 22 '25 03:10

steveukx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!