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.
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:
//m.sbmmt.com/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!