Getting url parameters inside html page

I have an html page that loads using a url that looks something like this:

http://localhost:8080/GisProject/MainService?s=C&o=1

I would like to get the parameters in the url without using jsp. This can be done with Javascriptor jQuery. I want to test my page using my local server Node.jsbefore deploying it to a remote computer that uses a Java server.

Is there a library that will allow me to do this.

+7
source share
3 answers

A good solution is given here :

function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}​

, , URL-, http://dummy.com/?technology=jquery&blog=jquerybyexample:

var tech = GetURLParameter('technology');
var blog = GetURLParameter('blog');`
+29

, URL, , :

https://sergiomcfly.com/index.php?myfirstname=sergio&mylastname=mcfly

JS...

urlp=[];u=location.search.replace("?","").split("&").forEach(function(d){e=d.split("=");urlp[e[0]]=e[1];})

:-)

document.write(urlp["myfirstname"]);
0

//http://localhost:8080/GisProject/MainService?s=C&o=1
const params = new URLSearchParams(document.location.search);
const s = params.get("s");
const o = params.get("o");
console.info(s); //show C
console.info(o); //show C
Run codeHide result
0
source

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


All Articles