Reputation: 149
Having an issue with the following code, it displays the full URL of the current page,
(eg, example.com/dir1) however I am looking to display the sub directories only (eg, /dir1/). Also having an issue where it does not display spaces properly, spaces show in html encoding %20
.
I have very little programming experience and any help would be greatly appreciated.
<p3><script>document.write(location.href);</script></p3>
EDIT
working on the following script, however am having trouble implementing it
<script>str.replace("%20", " ")</script>
any thoughts?
EDIT - answer which suited my needs, many thanks to brettc
var url = location.href;
url = url.split("examle.com").pop();
url = decodeURIComponent(url);
document.write(url);
Upvotes: 0
Views: 82
Reputation: 71
This may not be the best way, but a way none the less.
You could just split the string by the ”/“
and take the last occurrence.
Also, decodeURIComponent() will decode the %20 to a space, as found here.
<script>
var url = location.href;// get url, put in url variable
url = url.split("/").pop();// get last element separated by “/“
url = decodeURIComponent(url);// remove any %20
alert(url);
</script>
Note: this will alert everything past the last “/“.
Upvotes: 1