6

I need to get the id from the url. So if the url is something like that: http://localhost:17241/Chart.aspx?id=11

I should get the number 11 as an output. But there is a different id on every site. And after that i will set the z-index with that.

I've already tried to write something

$.urlParam = function (name) {
var patt1 = new RegExp('[^17241]').exec(window.location.href);
return result[1] || 0;
document.getElementById("link").innerHTML = result;}

But this doesn't work at all. Does anyone know what to do?

So now it should change the z-index:

var url = window.location.href;
var params = url.split('?');
var id = params[1].split('=')[1]; //params[1] will contain everything after ? 
console.log(id);
if(id == 11)
{
    $(“#one”).each(function() {
        $(this).css(“z-index“, 0);
    });

}

else if(id == 31)
{
    $(“#four”).each(function() {
        $(this).css(“z-index“, 0);
    });
}

And it should change the z-index of the divs

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<div class="contentwidth" id="chartdiv">
    <asp:Label ID="lblStatus" runat="server"></asp:Label>
    <div id="one" style="position: absolute; top: 0; left: 0; height: 100%; width: 100%;">
        <asp:Literal ID="ltrRenderChart" runat="server"></asp:Literal>
    </div>
    <div id="two" style="position: absolute; top: 0; left: 50%; height: 100%; width: 100%; z-index:-1">
        <asp:Literal ID="ltrRenderChart2" runat="server"></asp:Literal>
    </div>
    <div id="three" style="position: absolute; top: 50%; left: 0; height: 100%; width: 100%; z-index:-1">
        <asp:Literal ID="ltrRenderChart3" runat="server"></asp:Literal>
    </div>
    <div id="four" style="position: absolute; top: 50%; left: 50%; height: 100%; width: 100%; z-index:-1 ">
        <asp:Literal ID="ltrRenderChart4" runat="server"></asp:Literal>
    </div>
</div>

Thanks

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Fabio ha
  • 553
  • 1
  • 8
  • 29

10 Answers10

3

You can make use of split()

var url = 'http://localhost:17241/Chart.aspx?id=11'
var params = url.split('?');
var id=params[1].split('=')[1]; //params[1] will contain everything after ? 
console.log(id);

EDIT

To get the url inside the var url replace the first line with

var url = window.location.href;
Zee
  • 8,420
  • 5
  • 36
  • 58
3

It's called query string

here is the function,

 function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
 }

and how you call

getParameterByName(name)

Above code is from here How can I get query string values in JavaScript?

Community
  • 1
  • 1
user786
  • 3,902
  • 4
  • 40
  • 72
1

You can use regex:

alert(window.location.href.match(/\?id=(\d+)/i)[1]);
  1. \?: Match ?. need to escape by \
  2. id=: Matches exact string id=
  3. \d+: Matches any no. of digits
  4. (): Grouping.
  5. i: Case Insensitive match
Tushar
  • 85,780
  • 21
  • 159
  • 179
1

If you want it as an actual number I would write a generic version

function getParamAsNumber(url, param) {
    param = param + '=';
    if (url.indexOf(param) !== -1) {
        return parseInt(url.substr(url.indexOf(param) + param.length));
    }
}

It converts to integer the string after param + '=' (in your case 'id=')

So you can do

getParamAsNumber(window.location.href, 'id');
B3rn475
  • 1,057
  • 5
  • 14
1

I'm using this method to obtain any parameter from the URL of the current window:

function obtainParameter(key) {
    var result=null, tmp;
    window.location.search //this attribute stores the string found after a ‘?’ in the URL, including the question mark.
        .substr(1) //removing question mark in position 0
        .split("&") //obtaining the pairs key=value
        .forEach(function (item) {
            tmp = item.split("="); // tmp[0] is the parameter name, tmp[1] is the value
            if (tmp[0] === key) result = decodeURIComponent(tmp[1]);
        });
    return result;
}

Having it, you only need to write:

var id=obtainParameter('id');
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
1
var url = "http://localhost:17241/Chart.aspx?id=11";

url = url.split("=")[1];
Rajiv007
  • 1,126
  • 7
  • 13
1
var url = 'http://localhost:17241/Chart.aspx?id=11';
var match = url.match(/id=(\d+)/)
if (match) {
    var id = match[1];
}
peszo
  • 429
  • 1
  • 4
  • 16
1

You can use split function: string.split(separator,limit)

 var str = "http://localhost:17241/Chart.aspx?id=11";
 var res = str.split("=", 2);
 document.getElementById("link").innerHTML = res[1];
Ragu
  • 373
  • 5
  • 15
1

You don't need Regex for this small task. You can use the function below to get your requirements :

function getvalue(murl,para)
{
   try
   {
        return (murl.split(para+"=")[1]).split("&")[0];
   }
catch(e)
   {
      return '';
   }
}
var murl = 'http://localhost:17241/Chart.aspx?id=11';
var id = getvalue(murl,'id');

It will not return anything when the GET parameter is not found.

In your case :

var id = getvalue(window.location.href,'id');
IBAD GORE
  • 403
  • 2
  • 5
1

You might use this function for getting query string parameters with JS:

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split('&');
  for(var i=0; i<vars.length; i++) {
    var pair = vars[i].split('=');
    if(pair[0] == variable) {
      return pair[1];
    };
  }:
  return(false);
};

Then you can get the value of id like this:

var yourID = getQueryVariable('id');
Christian Lundahl
  • 2,000
  • 3
  • 18
  • 29