Rectangle 27 11

You realise the lastIndexOf('/') occurs as the last character of the URL/string?

javascript - jQuery get last part of URL - Stack Overflow

javascript jquery html url get
Rectangle 27 11

You realise the lastIndexOf('/') occurs as the last character of the URL/string?

javascript - jQuery get last part of URL - Stack Overflow

javascript jquery html url get
Rectangle 27 3

There is one best way to take last part of URL is like following which generally has been used in real implementation.

1.Consider what if there is a url like www.example.com/folder/code/12345 (Without '/' forward slash) Than none of the above code will work as per expectation.

www.example.com/folder/sub-folder/sub-sub-folder/code/12345
$(function () {
     siteCode = getLastPartOfUrl('www.example.com/folder/code/12345/');
});

var getLastPartOfUrl =function($url) {
    var url = $url;
    var urlsplit = url.split("/");
    var lastpart = urlsplit[urlsplit.length-1];
    if(lastpart==='')
    {
        lastpart = urlsplit[urlsplit.length-2];
    }
    return lastpart;
}

javascript - jQuery get last part of URL - Stack Overflow

javascript jquery html url get
Rectangle 27 3

There is one best way to take last part of URL is like following which generally has been used in real implementation.

1.Consider what if there is a url like www.example.com/folder/code/12345 (Without '/' forward slash) Than none of the above code will work as per expectation.

www.example.com/folder/sub-folder/sub-sub-folder/code/12345
$(function () {
     siteCode = getLastPartOfUrl('www.example.com/folder/code/12345/');
});

var getLastPartOfUrl =function($url) {
    var url = $url;
    var urlsplit = url.split("/");
    var lastpart = urlsplit[urlsplit.length-1];
    if(lastpart==='')
    {
        lastpart = urlsplit[urlsplit.length-2];
    }
    return lastpart;
}

javascript - jQuery get last part of URL - Stack Overflow

javascript jquery html url get
Rectangle 27 10

var URI = 'www.example.com/folder/code/12345/',
    parts = URI.split('/'),
    lastPart = parts.pop() == '' ? parts[parts.length - 1] : parts.pop();

Using .pop() confuses things as it's destructive and changes the parts[] array. If your URI does not have a trailing slash, this fails to return '12345' and instead returns 'code'. Using this version is more straight-forward: lastPart = parts[parts.length-1] !== '' ? parts[parts.length-1] : parts[parts.length-2];

javascript - jQuery get last part of URL - Stack Overflow

javascript jquery html url get
Rectangle 27 10

var URI = 'www.example.com/folder/code/12345/',
    parts = URI.split('/'),
    lastPart = parts.pop() == '' ? parts[parts.length - 1] : parts.pop();

Using .pop() confuses things as it's destructive and changes the parts[] array. If your URI does not have a trailing slash, this fails to return '12345' and instead returns 'code'. Using this version is more straight-forward: lastPart = parts[parts.length-1] !== '' ? parts[parts.length-1] : parts[parts.length-2];

javascript - jQuery get last part of URL - Stack Overflow

javascript jquery html url get
Rectangle 27 8

var str="url";

str.split("/")[3]

Thanks a lot, this got it working

javascript - jQuery get last part of URL - Stack Overflow

javascript jquery html url get
Rectangle 27 8

var str="url";

str.split("/")[3]

Thanks a lot, this got it working

javascript - jQuery get last part of URL - Stack Overflow

javascript jquery html url get
Rectangle 27 375

The following code will return a JavaScript Object containing the URL parameters:

// Read a page's GET URL variables and return them as an associative array.
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;
}

For example, if you have the URL:

http://www.example.com/?me=myValue&name2=SomeOtherValue
{
    "me"    : "myValue",
    "name2" : "SomeOtherValue"
}

and you can do:

var me = getUrlVars()["me"];
var name2 = getUrlVars()["name2"];

Note that solution doesn't unencode the parameter values ... and doesn't seem to explicitly handle # values in the url either? I'd suggest stackoverflow.com/questions/901115/ instead, like @Steve did

@Rory, yes: This: "stackoverflow.com?foo=bar#topic1 will give you {"foo" : "bar#topic"}. This is why poor guy asked for a well-known library's solution, presumably, hoping to find a solution that had had some testing done on it and covered all the bases (I'm saying that's what he was HOPING to find, not that jQuery would necessarily provide it).

Additionally, it returns an object - JavaScript does not have associative arrays. And opening curly braces should go on the same line as the expression in JavaScript. </superpickycomment>

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 372

The following code will return a JavaScript Object containing the URL parameters:

// Read a page's GET URL variables and return them as an associative array.
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;
}

For example, if you have the URL:

http://www.example.com/?me=myValue&name2=SomeOtherValue
{
    "me"    : "myValue",
    "name2" : "SomeOtherValue"
}

and you can do:

var me = getUrlVars()["me"];
var name2 = getUrlVars()["name2"];

Note that solution doesn't unencode the parameter values ... and doesn't seem to explicitly handle # values in the url either? I'd suggest stackoverflow.com/questions/901115/ instead, like @Steve did

@Rory, yes: This: "stackoverflow.com?foo=bar#topic1 will give you {"foo" : "bar#topic"}. This is why poor guy asked for a well-known library's solution, presumably, hoping to find a solution that had had some testing done on it and covered all the bases (I'm saying that's what he was HOPING to find, not that jQuery would necessarily provide it).

Additionally, it returns an object - JavaScript does not have associative arrays. And opening curly braces should go on the same line as the expression in JavaScript. </superpickycomment>

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 369

The following code will return a JavaScript Object containing the URL parameters:

// Read a page's GET URL variables and return them as an associative array.
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;
}

For example, if you have the URL:

http://www.example.com/?me=myValue&name2=SomeOtherValue
{
    "me"    : "myValue",
    "name2" : "SomeOtherValue"
}

and you can do:

var me = getUrlVars()["me"];
var name2 = getUrlVars()["name2"];

Note that solution doesn't unencode the parameter values ... and doesn't seem to explicitly handle # values in the url either? I'd suggest stackoverflow.com/questions/901115/ instead, like @Steve did

@Rory, yes: This: "stackoverflow.com?foo=bar#topic1 will give you {"foo" : "bar#topic"}. This is why poor guy asked for a well-known library's solution, presumably, hoping to find a solution that had had some testing done on it and covered all the bases (I'm saying that's what he was HOPING to find, not that jQuery would necessarily provide it).

Additionally, it returns an object - JavaScript does not have associative arrays. And opening curly braces should go on the same line as the expression in JavaScript. </superpickycomment>

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 369

The following code will return a JavaScript Object containing the URL parameters:

// Read a page's GET URL variables and return them as an associative array.
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;
}

For example, if you have the URL:

http://www.example.com/?me=myValue&name2=SomeOtherValue
{
    "me"    : "myValue",
    "name2" : "SomeOtherValue"
}

and you can do:

var me = getUrlVars()["me"];
var name2 = getUrlVars()["name2"];

Note that solution doesn't unencode the parameter values ... and doesn't seem to explicitly handle # values in the url either? I'd suggest stackoverflow.com/questions/901115/ instead, like @Steve did

@Rory, yes: This: "stackoverflow.com?foo=bar#topic1 will give you {"foo" : "bar#topic"}. This is why poor guy asked for a well-known library's solution, presumably, hoping to find a solution that had had some testing done on it and covered all the bases (I'm saying that's what he was HOPING to find, not that jQuery would necessarily provide it).

Additionally, it returns an object - JavaScript does not have associative arrays. And opening curly braces should go on the same line as the expression in JavaScript. </superpickycomment>

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 369

The following code will return a JavaScript Object containing the URL parameters:

// Read a page's GET URL variables and return them as an associative array.
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;
}

For example, if you have the URL:

http://www.example.com/?me=myValue&name2=SomeOtherValue
{
    "me"    : "myValue",
    "name2" : "SomeOtherValue"
}

and you can do:

var me = getUrlVars()["me"];
var name2 = getUrlVars()["name2"];

Note that solution doesn't unencode the parameter values ... and doesn't seem to explicitly handle # values in the url either? I'd suggest stackoverflow.com/questions/901115/ instead, like @Steve did

@Rory, yes: This: "stackoverflow.com?foo=bar#topic1 will give you {"foo" : "bar#topic"}. This is why poor guy asked for a well-known library's solution, presumably, hoping to find a solution that had had some testing done on it and covered all the bases (I'm saying that's what he was HOPING to find, not that jQuery would necessarily provide it).

Additionally, it returns an object - JavaScript does not have associative arrays. And opening curly braces should go on the same line as the expression in JavaScript. </superpickycomment>

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 130

location.search

This does not return the search string from a url string. It only returns the current page's current address bar search string value.

@Cheezmeister: location.search works in Chrome, Firefox and IE10+. Might work in IE9 and lower too, but I don't have one on hand. Dwight: window.location.search returns the query string. Try it on this page in the console and you'll see ?a=b&c=d.

There's still a bit of work to do after location.search is pulled. This is the quickest way to get you halfway there, though. You'll need to drop the "?" and split on the "&", then split each result on "=".

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 129

location.search

This does not return the search string from a url string. It only returns the current page's current address bar search string value.

@Cheezmeister: location.search works in Chrome, Firefox and IE10+. Might work in IE9 and lower too, but I don't have one on hand. Dwight: window.location.search returns the query string. Try it on this page in the console and you'll see ?a=b&c=d.

There's still a bit of work to do after location.search is pulled. This is the quickest way to get you halfway there, though. You'll need to drop the "?" and split on the "&", then split each result on "=".

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 129

location.search

This does not return the search string from a url string. It only returns the current page's current address bar search string value.

@Cheezmeister: location.search works in Chrome, Firefox and IE10+. Might work in IE9 and lower too, but I don't have one on hand. Dwight: window.location.search returns the query string. Try it on this page in the console and you'll see ?a=b&c=d.

There's still a bit of work to do after location.search is pulled. This is the quickest way to get you halfway there, though. You'll need to drop the "?" and split on the "&", then split each result on "=".

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 129

location.search

This does not return the search string from a url string. It only returns the current page's current address bar search string value.

@Cheezmeister: location.search works in Chrome, Firefox and IE10+. Might work in IE9 and lower too, but I don't have one on hand. Dwight: window.location.search returns the query string. Try it on this page in the console and you'll see ?a=b&c=d.

There's still a bit of work to do after location.search is pulled. This is the quickest way to get you halfway there, though. You'll need to drop the "?" and split on the "&", then split each result on "=".

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 129

location.search

This does not return the search string from a url string. It only returns the current page's current address bar search string value.

@Cheezmeister: location.search works in Chrome, Firefox and IE10+. Might work in IE9 and lower too, but I don't have one on hand. Dwight: window.location.search returns the query string. Try it on this page in the console and you'll see ?a=b&c=d.

There's still a bit of work to do after location.search is pulled. This is the quickest way to get you halfway there, though. You'll need to drop the "?" and split on the "&", then split each result on "=".

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 23

An easy way to do this with some jQuery and straight JS, just view your console in Chrome or Firefox to see the output...

var queries = {};
  $.each(document.location.search.substr(1).split('&'),function(c,q){
    var i = q.split('=');
    queries[i[0].toString()] = i[1].toString();
  });
  console.log(queries);

You may want to remove + and decodeURIComponent on the way... And use window.location.search instead

Handy! Note that if you want to handle empty/null querystring elements, you'll want to add an 'if (i[0].length > 0)...' block...

Also, can't believe this required 5 lines of javascript to do. What year is it??

You shouldn't split on '=', it's technically okay to have a second equals sign in the value of any key/value pair. -- Only the first equals sign you encounter (per key/value pair) should be treated as a delimiter. (Also, the equals sign isn't strictly required; it's possible to have a key with no value.)

this solution does not take into account arrays in the query, e.g. filters[class]=Gym

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string
Rectangle 27 22

An easy way to do this with some jQuery and straight JS, just view your console in Chrome or Firefox to see the output...

var queries = {};
  $.each(document.location.search.substr(1).split('&'),function(c,q){
    var i = q.split('=');
    queries[i[0].toString()] = i[1].toString();
  });
  console.log(queries);

You may want to remove + and decodeURIComponent on the way... And use window.location.search instead

Handy! Note that if you want to handle empty/null querystring elements, you'll want to add an 'if (i[0].length > 0)...' block...

Also, can't believe this required 5 lines of javascript to do. What year is it??

You shouldn't split on '=', it's technically okay to have a second equals sign in the value of any key/value pair. -- Only the first equals sign you encounter (per key/value pair) should be treated as a delimiter. (Also, the equals sign isn't strictly required; it's possible to have a key with no value.)

this solution does not take into account arrays in the query, e.g. filters[class]=Gym

javascript - jquery get querystring from URL - Stack Overflow

javascript jquery query-string