PHP has $_GET variable, what does HTML have? [dupl

2019-09-14 11:33发布

This question already has an answer here:

I know if I have a php page, I can pass variables to the next page through the url with $_GET. Such as:

http://www.w3schools.com/welcome.php?fname=Peter&age=37

yields:

$currentName = $_GET["fname"]
$currentAge = $_GET["age"]

Is there an equally easy way to do this in pure HTML (or javascript)?

1条回答
做自己的国王
2楼-- · 2019-09-14 11:48

using jQuery you can get url parameters with window.location but parsing with regex

$.urlParameter = function(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}

$.urlParameter('Parameter  name');

OR with JS

function urlParameter(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}

urlParameter('Parameter  name');
查看更多
登录 后发表回答