Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a number out of the url [duplicate]

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

like image 933
Fabio ha Avatar asked May 27 '15 12:05

Fabio ha


2 Answers

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;
like image 166
Zee Avatar answered Oct 21 '22 22:10

Zee


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?

like image 36
user786 Avatar answered Oct 21 '22 20:10

user786