Home >Web Front-end >JS Tutorial >How to get the address bar parameters in js?

How to get the address bar parameters in js?

零下一度
零下一度Original
2017-06-28 13:27:101783browse

This article mainly introduces the two methods of js to obtain the address bar parameters in detail. It has certain reference value. Interested friends can refer to it.

Frequently encountered problems in obtaining the address bar parameters in the project. Jump to a page to get the current parameters

//获取地址栏参数,name:参数名称
 function getUrlParms(name){
   var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
   var r = window.location.search.substr(1).match(reg);
   if(r!=null)
   return unescape(r[2]);
   return null;
   }
var id = getUrlParms("id");

The above method can get the id passed from the address bar, you can also write like this


function getRequest() {
  var url = window.location.search; //获取url中"?"符后的字串
  var theRequest = new Object();
  if (url.indexOf("?") != -1) {
    var str = url.substr(1);
    strs = str.split("&");
    for(var i = 0; i < strs.length; i ++) {
      
      theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
      
    }
  }
  return theRequest;
}
var id= getRequest().id;

The above is the detailed content of How to get the address bar parameters in js?. 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