开发者

jquery get var from uri

I have two page page1.php and page2.php, on page1.php i click on a href and that link to page2.php somewhere...

in the page2.php there is 4 expander, hidden or close on pageload...

question : i like to pass from page 1 to page2 which of the expander i like to see OPEN

something like page1 href="page2.php?open=1"

and on page2.php a jquery that retreive the open var, and after pageload expand only that on开发者_JAVA百科e...

i have research google and come with that : https://github.com/allmarkedup/jQuery-URL-Parser

is it possible to do that without plugsin ?


The specific answer:

var expanderNo = (location.search.match(/open=(\d+)/) || [])[1]

But if your intention here is to link to a specific part of the page, then URL fragment is more appropriate. Given the URL /page2.php#expander1 you can retrieve the expander1 part with location.hash.substr(1).


Here is one generic way of doing it, probably not the best, but I had the code on hand, so thought I'd share.

    function getVars(){
        var vars = new Array();
        var query = window.location.search.substring(1);
        var parms = query.split('&');
        for (var i = 0; i < parms.length; i++) {
                var pos = parms[i].indexOf('=');
                if (pos > 0) {
                        var key = parms[i].substring(0, pos);
                        var val = parms[i].substring(pos + 1);
                        vars[key] = val;
                }else{
                        return Array();
                }
        }
        return vars;
   }

it will return a key value pair for each parameter.

The other answer (Alexey's) is better if you know the parameters beforehand.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜