Home  >  Article  >  Web Front-end  >  How to get url parameters in javascript

How to get url parameters in javascript

藏色散人
藏色散人Original
2021-04-08 14:44:2222527browse

How to get url parameters in javascript: first create a js sample file; then define a "getQueryVariable" function; finally get the url parameters by calling this function.

How to get url parameters in javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript gets url parameters

The following JS function is used to get url parameters:

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

Usage example

url example:

http://www.php.cn/index.php?id=1&image=awesome.jpg

Call getQueryVariable("id") to return 1.

Call getQueryVariable("image") to return "awesome.jpg".

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to get url parameters in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn