JS获取URL参数

对于纯静态Html要获取url参数的值只能通过JS实现,如a.html?id=3,要获取id的值,可通过以下方法实现:

 function QueryString(_name_) {  

if (location.search.length == 0 || _name_.length == 0) { return ''; } 

var _search_ = location.search.replace('?', ''); 

var _name_ = _name_ + '='; if (_search_.indexOf('&') >= 0) { _search_ = _search_.split('&'); 

var _value_ = ''; for (_index_ = 0; _index_ < _search_.length; _index_++) { if (_search_[_index_].indexOf(_name_) >= 0) { _value_ = _search_[_index_]; continue

; } } } else { _value_ = _search_; } _value_ = _value_.indexOf(_name_) >= 0 ? _value_.replace(_name_, '') : ''; _search_ = null; _name_ = null; _index_ = null

return_value_; }

 

方法调用 :

var id= QueryString("id")

 就可获取参数id的值

你可能感兴趣的:(url)