简体   繁体   中英

how to get first argument from url in javascript


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.

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

update:

in case there's a variable holding url:

url.substring(url.lastIndexOf("?") + 1).split("&")[0];

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]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM