开发者

javascript: parsing variables in the URL

开发者 https://www.devze.com 2023-01-13 13:06 出处:网络
so, my URL is example.com/?ref=person but ref is null after the regex. What am I doing wrong here? function getReferer(){

so, my URL is example.com/?ref=person

but ref is null after the regex. What am I doing wrong here?

function getReferer(){
    var regex = new RegExp(/ref=(.+)/);
    var ref = regex开发者_运维百科.exec(window.location.ref);
    alert(ref);
    if (ref == null) return "";
    else return ref[1];
}


Replace window.location.ref by window.location.href. Don't use new RegExp if not necessary, it's slower.

function getReferer(){
    var regex = /ref=(.+)/;
    var ref = regex.exec(window.location.href);
    alert(ref);
    if (ref == null) return "";
    else return ref[1];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消