Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a parameter to a javascript through a url and display it on a page?

For instance, I have a url like this : www.mysite.com/my_app.html

How do I pass a value " Use_Id = abc " to it and use javascript to display on that page ?

like image 487
Frank Avatar asked Mar 08 '10 22:03

Frank


1 Answers

Shouldn't be too difficult to write your own without the need for an external library.

// www.mysite.com/my_app.html?Use_Id=abc 

var GET = {};
var query = window.location.search.substring(1).split("&");
for (var i = 0, max = query.length; i < max; i++)
{
    if (query[i] === "") // check for trailing & with no param
        continue;

    var param = query[i].split("=");
    GET[decodeURIComponent(param[0])] = decodeURIComponent(param[1] || "");
}

Usage: GET.Use_id or GET["Use_id"]. You can also check if a parameter is present even if it has a null value using "Use_id" in GET (will return true or false).

like image 191
Andy E Avatar answered Sep 29 '22 11:09

Andy E