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/
Related
Any way to extract just a portion of a full path from a URL using JavaScript?
Example:
I have URL
http://www.somedomain.com/account/6099bb4e2eb24bab85e8b76851a14260/search?filter=a#top
but I would just like to get this portion
6099bb4e2eb24bab85e8b76851a14260
This is a random id
few examples like:
6099bb4e2eb24bab85e8b76851a14260,
93b04efd1a734407b28f95d9c5278c0f,
94c9483fdb3747b38e4836fe9294cbfe,
93b04efd1a734407b28f95d9c5278c0f
Please help me out...its driving me nutts.
Learning....
You could go about doing it like this,
const url=new URL('http://www.somedomain.com/account/6099bb4e2eb24bab85e8b76851a14260/search?filter=a#top');
/*
url.pathname="/account/6099bb4e2eb24bab85e8b76851a14260/search"
url.pathname.split('/')=["","account","6099bb4e2eb24bab85e8b76851a14260","search"]
*/
const accountId=url.pathname.split('/')[2];
console.log(accountId);
Refer this for more on how to use the URL object in JS,
https://developer.mozilla.org/en-US/docs/Web/API/URL
This should get you the desired result.
function myFunction() {
var url = "http://www.somedomain.com/account/6099bb4e2eb24bab85e8b76851a14260/search?filter=a#top";
var result = str.split('/')[4];
console.log(result);
}
This is a simple example of splitting the url string and selecting the position of the desired value
I need the whole parameter list as such , not one by one
var Url = "http://localhost/Home/Admin?param1=1¶m2=2$param3=3";
I want to get the whole parameter list from the url.
var params = "param1=1¶m2=2¶m3=3";
var Url = "http://localhost/Home/Admin?param1=1¶m2=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];
I have a website like below:
localhost:3000/D129/1
D129 is a document name which changes and 1 is section within a document.
Those two values change depends on what user selects.
How do I just extract D129 part from the URL using javascript?
window.location.pathname.match(/\/([a-zA-Z\d]*)/)[1]
^ that should get you the 1st string after the slash
var path = "localhost:3000/D129/1";
alert(path.match(/\/([a-zA-Z\d]*)/)[1])
You can use .split() and [1]:
a = "localhost:3000/D129/1";
a = a.split("/");
alert(a[1]);
This works if your URLs always have the same format. Better to use RegEx. Wanted to answer in simple code. And if you have it with http:// or something, then:
a = "http://localhost:3000/D129/1";
a = a.split("/");
alert(a[3]);
ps: For the RegEx version, see Tuvia's answer.
I want to get the my exact location (only PHP page) using javascript, I'm currently using window.location to get the exact url for example: localhost/empc/myfolder/functions.php what is the code if I want to get only functions.php as a result? Is it possible? Thank you.
You can use location object to do this, To remove the "/" before the file name you can use substring method.
location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
you can try this pretty simple
var url = 'localhost/empc/myfolder/functions.php';
var url = url.split('/');
alert(url.pop());
It is possible. Use the javascript split function to separate the string into pieces and then access the last element to get the file name:
var str = "localhost/empc/myfolder/functions.php";
var arr = str.split("/");
alert(arr[arr.length-1]); //this will alert functions.php
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.