Get a variable from url parameter using Javascript

后端 未结 4 2068
旧时难觅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:35

    Your approach should work. Here's my version for comparison:

    var o = {
        keys: [ ],
       values: [ ]
    }
    
    /* 
    You could just use the window#location#search value to get the query sub-String of the currently loaded URL
    
     var q = window.location.search.substring(1) ; 
    
    For now we'll use a query String from a Google search for "MDN window location"
    */
    
    q = "q=mdn+window+location&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:de:official&client=firefox-a"
    
    for( var i = 0 , a = q.split("&"), p ; i < a.length ; i++ ) {
        p = a[i] ;
        if(  ( b = p.split("=") )  !=  null ) {
            o.keys[i] = b[0] ;
            o.values[i] = b[1] ;
        }
    }
    
    
    console.log("(!!) o: " + o.toSource( ) ) ;
    

    • try it on JSFiddle

提交回复
热议问题