How can I get the path name from the below URL?
file:///index.html/temp/data
I understand that /temp/data
is the pathname.
window.location.pathname
gives me index.html/temp/data
How can I get the path name from the below URL?
file:///index.html/temp/data
I understand that /temp/data
is the pathname.
window.location.pathname
gives me index.html/temp/data
Unfortunately if your url is file:///index.html/temp/data then the pathname will be everything after file://, so in your case it should return /index.html/temp/data.
I don't think there is a build in solution to handle this.
To solve the problem above you can try to do
window.location.pathname.split('.html/')[1]
You can use RegExp.match
to get the desired path
let [path] = 'index.html/temp/data'.match(/(\/\w*)+/);
console.log(path)
let [path2] = 'index.htm/temp/data'.match(/(\/\w*)+/);
console.log(path2)
Try this:
console.log (new URL("file:///index.html/temp/data")).
This will log an object with various properties such as hostname
and pathname
which I think is what you looking for.
Hope it helps!
If that is the page you have loaded, you should be able to use
location.pathname
to give you the path.
Otherwise, if that is just a string you have string.split('/')[3]
but you should probably do some validation to make sure it is always after the 4th '/'