8

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 ?

Frank
  • 30,590
  • 58
  • 161
  • 244

6 Answers6

18

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).

Andy E
  • 338,112
  • 86
  • 474
  • 445
6

Call the page www.mysite.com/my_app.html?Use_Id=abc

Then in that page use a javascript function like:

var urlParam = function(name, w){
    w = w || window;
    var rx = new RegExp('[\&|\?]'+name+'=([^\&\#]+)'),
        val = w.location.search.match(rx);
    return !val ? '':val[1];
}

To use it:

var useId = urlParam('Use_Id');

The second parameter w is optional, but useful if you want to read parameters on iframes or parent windows.

Mic
  • 24,812
  • 9
  • 57
  • 70
  • Your code is pretty useful! Thank, you. What about post method like in php? – SphynxTech Nov 26 '17 at 13:13
  • @S.P.H.I.N.X when you make a post to a PHP page, there is no client javascript anymore. You need to do a parsing with PHP on the server either data posted or in the url like here. – Mic Nov 27 '17 at 13:45
2

www.mysite.com/my_app.html?use_id=abs

var qs = new QueryString()

// use_id is now available in the use_id variable
var use_id = qs.get("use_id");
isherwood
  • 58,414
  • 16
  • 114
  • 157
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
  • QueryString Code can be found at https://web.archive.org/web/20101025074108/http://adamv.com/dev/javascript/files/querystring.js – SESN Feb 04 '17 at 18:53
1
www.mysite.com/my_app.html?Use_Id=abc


var querystring = window.location.querystring;
var myValue = querystring["Use_Id"];

http://prettycode.org/2009/04/21/javascript-query-string/

isherwood
  • 58,414
  • 16
  • 114
  • 157
Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
0

There's a similar question on this here What is the easiest way to read/manipulate query string params using javascript?

Which seems to recommend using jQuery's Querystring plugin: http://plugins.jquery.com/project/query-object

Community
  • 1
  • 1
Ceilingfish
  • 5,397
  • 4
  • 44
  • 71
0

Your server side script could read the value and set it in a hidden form field which your JS could then read.