Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the last part of URL javascript?

Tags:

javascript

I need to display the last part of a URL using javascript!

I am using this code but this will display the entire URL:

<script language="javascript">

document.writeln(document.location);
var url = $(this).attr("href");
var part = url.substring(url.lastIndexOf('/') + 1);

</script>

if the URL look like this:

domain.com/something/file

i need to only display the "file".

like image 865
Simon Presto Avatar asked Aug 12 '13 19:08

Simon Presto


People also ask

How do I get the current URL?

Answer: Use the window. location. href Property You can use the JavaScript window. location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc. The following example will display the current url of the page on click of the button.

What is window location pathname?

window.location.pathname returns the path and filename of the current page. window.location.protocol returns the web protocol used (http: or https:) window.location.assign() loads a new document.

What is URL in JavaScript?

url. A string or any other object with a stringifier — including, for example, an <a> or <area> element — that represents an absolute or relative URL. If url is a relative URL, base is required, and will be used as the base URL. If url is an absolute URL, a given base will be ignored. base Optional.


2 Answers

The reason document.write(window.location) writes the location is because of the toString method of window.location, which really returns window.location.href.

// This will fallback to the location.pathname if this
// is not the location already or not an anchor.
var path = this.pathname || window.location.pathname;
var part = path.split('/').pop();

Pathname is everything after the domain name. So, http://example.com/something/file breaks down like this:

  1. protocol: http:
  2. hostname: example.com
  3. pathname: something/file
  4. href: http://example.com/something/file

(there is also port, search (?this=that) and hash (#hash), which would both be empty in this case)

So, I'm taking something/file and splitting it into an array wherever this is a /, which would be ["something", "file"]

After that I'm popping off the last part of the array, in this case "file"


Both window.location and any <a> tag have these properties. So, if you need to parse a URL, you can do the following in javascript:

var anchor = document.createElement('a');
anchor.href = '/about'; // this could be any relative or absolute url

And now anchor will have the all those properties if you need them. No need for a regex or anything.


UPDATE

In newer browsers (excluding IE unless you use url-polyfill), you can use URL instead of an <a /> like so:

const url = new URL('/about', this.location)
// or if you don't care about the host, you can do the following
// const url = new URL('http://localhost/about')

This contains all the other information, plus url.searchParams, which makes it so you don't have to parse the search string yourself either.

like image 96
kalley Avatar answered Sep 22 '22 01:09

kalley


<script type="text/javascript">

var segment_str = window.location.pathname; // return segment1/segment2/segment3/segment4
var segment_array = segment_str.split( '/' );
var last_segment = segment_array.pop();
document.write(last_segment); // alerts segment4

</script>

JsFiddle: http://jsfiddle.net/HNMV3/1/

like image 26
Ali Gajani Avatar answered Sep 20 '22 01:09

Ali Gajani