顯示 URL javascript 的最後一部分? (Display the last part of URL javascript?)


問題描述

顯示 URL javascript 的最後一部分? (Display the last part of URL 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".


參考解法

方法 1:

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.

方法 2:

<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/

方法 3:

var pathname = window.location.pathname,
    part = pathname.substr(pathname.lastIndexOf('/') + 1);

方法 4:

replace(/‑/g," ") and split(".html") will remove "hyphens" and ".html" from url,thus only keeping the  path name only

var parts=window.location.pathname.split("/");
var query=parts[parts.length‑1].split(".html");
query[0]=query[0].replace(/‑/g," ");
document.write(query[0])

(by Simon PrestokalleyAli Gajanifederico‑tAbhishek Sen)

參考文件

  1. Display the last part of URL javascript? (CC BY‑SA 3.0/4.0)

#javascript






相關問題

為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)

在 Javascript 中打開外部 sqlite3 數據庫 (Open external sqlite3 database in Javascript)

Javascript:數組中的所有對像都具有相同的屬性 (Javascript: All Objects in Array Have Same Properties)

為什麼我們要在 javascripts 原型中添加函數? (Why do we add functions to javascripts prototype?)

顯示 URL javascript 的最後一部分? (Display the last part of URL javascript?)

Javascript XMLHttpRequest:忽略無效的 SSL 證書 (Javascript XMLHttpRequest: Ignore invalid SSL Certificate)

有沒有辦法測試 console.log 整體 (Is there a way to test for console.log entires)

如何從 javascript 對像中獲取名稱和值到新列表中? (How to get name and values from a javascript object into a new list?)

數據未發布..幫助!html,js,firebase (Data not posting.. Help! html,js,firebase)

使用 Node.js 腳本查看表單數據 (Seeing form data with Node.js script)

使用百分比查找範圍內的值 (find the value within a range using percent)

如何通過 react.js 中的組件傳遞變量或數據? (How to pass varible or data through components in react.js?)







留言討論