SoFunction
Updated on 2025-03-06

js written test questions - receive get request parameters

topic

Please write a JavaScript function, its purpose is to receive the parameters of the get request in the url and return it as an object.

For example: var url = “/?opt=1″.

This question examines how to obtain url as a string by JS and how to process strings.

1. First think of the rules

function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = (1).match(reg);
if (r != null) return unescape(r[2]); return null;
}

2. Array method

*-----------------accomplish1--------------------*/
function GetRequest() {
var url = ; //Get the string after the "?" character in the urlvar theRequest = new Object();
if (("?") != -1) {
var str = (1);
strs = ("&");
for(var i = 0; i < ; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
/*-----------------------------------------------------------------------------------------------------------------------------
var $_GET = (function(){
var url = ();
var u = ("?");
if(typeof(u[1]) == "string"){
u = u[1].split("&");
var get = {};
for(var i in u){
var j = u[i].split("=");
get[j[0]] = j[1];
}
return get;
} else {
return {};
}
})();
/*The second method, when using it, you can directly obtain the value of the GET parameter*/

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.