Home > Web Front-end > JS Tutorial > body text

How to get url parameters in javascript

藏色散人
Release: 2023-01-04 09:35:13
Original
22533 people have browsed it

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);
}
Copy after login

Usage example

url example:

//m.sbmmt.com/index.php?id=1&image=awesome.jpg
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template