Retrieving a Number from a URL

I need to get the id from the url. Therefore, if the URL looks something like this: http: // localhost: 17241 / Chart.aspx? Id = 11

I should get number 11 as output. But each site has a different identifier. And after that I will set the z-index to this.

I 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 does not work at all. Does anyone know what to do?

So now it should change 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 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

+6
source share
10 answers

You can use 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 var url , replace the first line with

 var url = window.location.href; 
+3
source

It is called a 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 what do you call

 getParameterByName(name) 

The above code is from here. How to get query string values ​​in JavaScript?

+3
source

You can use regex :

 alert(window.location.href.match(/\?id=(\d+)/i)[1]); 
  • \? : match ? . need to escape to \
  • id= : Matches the exact string id=
  • \d+ : matches any type. numbers
  • () : grouping.
  • i : Case insensitive
+1
source

If you want it to be like an actual number, I would write a general version

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

It will convert an integer string after param + '=' (in your case, 'id =')

So you can do

 getParamAsNumber(window.location.href, 'id'); 
+1
source

I use this method to get 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; } 

In this case, you only need to write:

 var id=obtainParameter('id'); 
+1
source
 var url = "http://localhost:17241/Chart.aspx?id=11"; url = url.split("=")[1]; 
+1
source
 var url = 'http://localhost:17241/Chart.aspx?id=11'; var match = url.match(/id=(\d+)/) if (match) { var id = match[1]; } 
+1
source

You can use the 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]; 
+1
source

You do not 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'); 
+1
source

You can use this function to get query string parameters using 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 id value as follows:

 var yourID = getQueryVariable('id'); 
+1
source

Source: https://habr.com/ru/post/988008/


All Articles