Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get data from url with jquery

Is it possible to get data from an url with jquery

for example if you have www.test.com/index.html?id=1&name=boo

how to get id and name?

like image 807
bdz Avatar asked Sep 17 '12 09:09

bdz


People also ask

How get data attribute value in jQuery?

To retrieve a data-* attribute value as an unconverted string, use the attr() method. Since jQuery 1.6, dashes in data-* attribute names have been processed in alignment with the HTML dataset API. $( "div" ).

How can I get specific data from AJAX response?

You can't as it's asynchronous. If you want to do anything with it, you need to do it in a callback. How? Because it's asynchronous, javascript will fire off the ajax request, then immediately move on to execute the next bit of code, and will probably do so before the ajax response has been received.

How jQuery read data from JSON file?

The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.


1 Answers

Try this. It's pure javascript, no jQuery involved. In fact jQuery is too heavy for such a job, really.

function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++)
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam)
        {
            return decodeURIComponent(sParameterName[1]);
        }
    }
}​

var id = GetURLParameter('id');
var name= GetURLParameter('name');

decodeURIComponent should be used to allow parameter value contain any character, for example the very crucial equals sign =, an ampersand & or a question mark ?.

like image 141
Alessandro Minoccheri Avatar answered Oct 25 '22 11:10

Alessandro Minoccheri