How to get URL parameter using jQuery or plain JavaScript?

前端 未结 30 3213
天涯浪人
天涯浪人 2020-11-21 06:29

I have seen lots of jQuery examples where parameter size and name are unknown.

My URL is only going to ever have 1 string:

http://example.com?sent=ye         


        
30条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 06:59

    This might be overkill, but there is a pretty popular library now available for parsing URIs, called URI.js.

    Example

    var uri = "http://example.org/foo.html?technology=jquery&technology=css&blog=stackoverflow";
    var components = URI.parse(uri);
    var query = URI.parseQuery(components['query']);
    document.getElementById("result").innerHTML = "URI = " + uri;
    document.getElementById("result").innerHTML += "
    technology = " + query['technology']; // If you look in your console, you will see that this library generates a JS array for multi-valued queries! console.log(query['technology']); console.log(query['blog']);
    
    
    

提交回复
热议问题