Get a variable from url parameter using Javascript

后端 未结 4 2002
旧时难觅i
旧时难觅i 2020-12-17 00:56

I am trying to get a url value using javascript, so far I can only get pure numbers, not mixed numbers with letters or just letters. I can\'t find any working examples of a

4条回答
  •  -上瘾入骨i
    2020-12-17 01:46

    I use this function, and it rocks. I don't remember from where I took it, but it's a good one:

    function gup(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null)
            return "";
        else
            return results[1];
    } 
    

    If you have a URL like http://www.exmaple.com/path?p1=lkjsd234&p2=klsjd987, you can use:

    alert(gup('p1')); // shows 'lkjsd234';
    

提交回复
热议问题