最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get a variable from url parameter using Javascript - Stack Overflow

programmeradmin5浏览0评论

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 function that allows for numbers with letters to be retrieved, just numbers. I am not using any non alphanumeric characters. An example value that I am trying to pass is "42p316041610751874cm83p2306600132141".

function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
}
return vars;
}

 var first = getUrlVars()["test"];

Any help would be great. Thanks.

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 function that allows for numbers with letters to be retrieved, just numbers. I am not using any non alphanumeric characters. An example value that I am trying to pass is "42p316041610751874cm83p2306600132141".

function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
}
return vars;
}

 var first = getUrlVars()["test"];

Any help would be great. Thanks.

Share Improve this question asked Dec 10, 2011 at 22:05 cWoolfcWoolf 631 gold badge1 silver badge4 bronze badges 4
  • That's odd. Your code should work. – Blender Commented Dec 10, 2011 at 22:07
  • I tried it, and it works for me. I used this URL: http://localhost/test/index.html?test=42p316041610751874cm83p2306600132141. – Brigand Commented Dec 10, 2011 at 22:14
  • I am using jQuery Mobile as the framework for the site if that matters? – cWoolf Commented Dec 10, 2011 at 23:16
  • Similar question? stackoverflow./questions/901115/… – Timo Huovinen Commented Sep 24, 2013 at 8:32
Add a ment  | 

4 Answers 4

Reset to default 6

Here is a condensed version of two of the answers above :

function gup (name) {
  name = RegExp ('[?&]' + name.replace (/([[\]])/, '\\$1') + '=([^&#]*)');
  return (window.location.href.match (name) || ['', ''])[1];
}

Easier to type, easier on processing and, IMHO easier to read. YMMV

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./path?p1=lkjsd234&p2=klsjd987, you can use:

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

Your code looks like it should work I use the function below if it helps

 function getParam(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];  
}

var first = getParam("test");

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

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
发布评论

评论列表(0)

  1. 暂无评论