Get the url Parameter string from Javascript - javascript

I need the whole parameter list as such , not one by one
var Url = "http://localhost/Home/Admin?param1=1&param2=2$param3=3";
I want to get the whole parameter list from the url.
var params = "param1=1&param2=2&param3=3";

var Url = "http://localhost/Home/Admin?param1=1&param2=2$param3=3";
var urlArray = url.split("?");
var params=urlArray[1];
You can see Using split() example of Mozilla Developer Network for more insight on using the split function.

Thanks for the support, I use this one for my need
var params = window.location.href.split('?')[1];

Related

Variable more than 1 URL in JavaScript

I want to set more than 1 url variable in JavaScript.
I am preparing a map, where only 1 url has been linied. I want to have more than 1.
I tried to use code like this:
var url = 'Peterborough.json';
var url = 'test.json';
but unfortunately only 2nd one is working. First one looks like switched off.
Does anyone knows how to place more than 1 url in the 1 line, to make them both working?
Thanks
A variable can have only one value at a certain time, what about using an array instead:
var url = ['Peterborough.json', 'text.json'];
console.log(url[0]); // => 'Peterborough.json'
console.log(url[1]); // => 'text.json'
You cannot declare two variables with the same name. You have two options here:
1- Simply renaming one of your variables, e.g.:
var url = 'Peterborough.json';
var url2 = 'test.json';
2- Use an array:
var urls = ["Peterborough.json", "test.json"];
//here urls[0] will be "Peterborough.json" (the first element of the array)
//and urls[1] will be "test.json" (the second element of the array)
You can do this:
var url0 = 'Peterborough.json';
var url1 = 'test.json';
console.log(url0);
console.log(url1);
I hoped it helped!

javascript - how to remove uri from a link

I got a link lie this
url = 'http//mysite.com/product/id/122?u=12'
I want to turn that to this
'http//mysite.com'
remove all the uri and query string
How you guys do it?
You can use URL api
let urlParsed = new URL("http://example.com/product/id/122?u=12")
let {origin} = urlParsed
console.log(origin)
Use split and string concatenation
var url = 'http//mysite.com/product/id/122?u=12'
console.log(url.split('.com/')[0]+'.com')

Get values of parameters from URL

I’m working on a tool which takes the value parameters in the URL and does a few things with them.
My issue is, I can’t seem to use document.location to show the specific value that I’m after, for example:
www.examplesite.com?yourname=gilgilad
I want to use document.location.search and put it in a var, I need that var's value to be "gilgilad".
Is this even possible using location.search?
location.search will return all after question mark including it. So there is universal js to get value of the first parameter (even if url has more parameters):
var desire = location.search.slice(1).split("&")[0].split("=")[1]
Example: let's take url http://example.com?name=jon&country=us
location.search will be equal to ?name=jon&country=us
.slice(1) skips the ?, returning the rest of the string.
.split("&")[0] splits it into two strings (name=jon and
country=us) and takes first one
.split("=")[1] splits name=jon into name and jon and takes the second one. Done!
let url = new URL('www.examplesite.com?yourname=gilgilad');
let searchParams = new URLSearchParams(url.search);
console.log(searchParams.get('yourname'));
you can consider also to user window.location or window.location.search directly
let searchParams = new URLSearchParams(window.location.search);
console.log(searchParams.get('yourname'));
A more generic solution to split the location.search query parameters and convert them into an object:
var a = location.search.split("&");
var o = a.reduce(function(o, v) {
var kv = v.split("=");
kv[0] = kv[0].replace("?", "");
o[kv[0]] = kv[1];
return o;
},
{});
To make ?yourname=gilgilad using document.location.search:
window.location.search = 'yourname=gilgilad';
here is jsfiddle: http://jsfiddle.net/t81k3bgc/
make sure to use console and then [run]. you will see:
For more information:
https://developer.mozilla.org/en-US/docs/Web/API/Window.location#Example_.235.3A_Send_a_string_of_data_to_the_server_by_modifying_the_search_property.3A

javascript/jquery grab part of URL

I have for example, https://www.example.com/test1/something?asd=1. Of this example URL I need to grab everything up until and included /test1/. So I would set var url = https://www.example.com/test1. The problem is that test1 is dynamic so I can not have any hard coded values.
How can I do this?
One way is to use a combination of split() and join():
var url = "https://www.example.com/test1/something?asd=1";
var result = url.split("/",4).join("/");
Here's a JSFiddle of it in action:
http://jsfiddle.net/msm3jsvw/

Get specific part of url of a link

I want to get a specific part of a url between the third and fourth slashes of a link on the page.
EDIT: Sorry I don't think I was clear the first time, I meant getting the specific part of the url OF A LINK found on the page.
var getSegment = function (url, index) {
return url.replace(/^https?:\/\//, '').split('/')[index];
}
Usage:
getSegment("http://domain.com/a/b/c/d/e", 4); // "d"
The replace makes sure that the first two slashes after the protocol (http or https) don't count.
Here's a working example of getting a particular path segment.
Code:
var url = "www.test.com/one/two/three?p1=v&p2=v#anc";
var file = url.split('?')[0];
var pathanddomain = file.split('/');
var path = pathanddomain.splice(1, pathanddomain.length-1);
var pathIndexToGet = 2;
document.write(path[pathIndexToGet]);​
If you want to do this for the current page, use:
var url = window.location.href;
Also, if your url starts with http(s)://, you will need to remove this.
I'd suggest:
var link = 'http://www.example.com/directory1/directory2/directory3/directory4/index.html';
console.log(link.split('/')[5]);​
JS Fiddle demo.
The reason we're using [5] not [4] is because of the two slashes at the beginning of the URL, and because JavaScript arrays are zero-based.
you should elaborate you question and should specify which is your domain, that means on what purpose you are asking that question ??
This may help you:
var urlValue = url.split("/");
Then store urlValue as array.
then pick up third and forth value of the urlvalue on array.

Categories