簡體   English   中英

用JavaScript獲取當前的URL?

[英]Get the current URL with JavaScript?

我只想獲取網站 URL。而不是從鏈接中獲取的 URL。 在頁面加載時,我需要能夠獲取網站的完整當前 URL 並將其設置為一個變量以供我隨意使用。

利用:

window.location.href

如評論中所述,下面的行有效,但它在 Firefox 中存在錯誤。

document.URL

請參閱DOMString 類型的 URL,只讀

URL 信息訪問

JavaScript 為您提供了許多檢索和更改當前 URL 的方法,該 URL 顯示在瀏覽器的地址欄中。 所有這些方法都使用Location對象,它是Window對象的一個​​屬性。 您可以通過閱讀window.location來閱讀當前的Location對象:

var currentLocation = window.location;

基本 URL 結構

<protocol>//<hostname>:<port>/<pathname><search><hash>
  • 協議:指定用於訪問 Internet 上的資源的協議名稱。 (HTTP(無 SSL)或 HTTPS(有 SSL))

  • 主機名:主機名指定擁有資源的主機。 例如, www.stackoverflow.com 服務器使用主機名提供服務。

  • 端口:一個端口號,用於識別 Internet 或其他網絡消息到達服務器時要轉發到的特定進程。

  • 路徑名:路徑提供有關 Web 客戶端想要訪問的主機中的特定資源的信息。 例如, /index.html

  • 搜索:查詢字符串跟隨路徑組件,並提供資源可用於某些目的的信息字符串(例如,作為搜索的參數或作為要處理的數據)。

  • hash: URL 的錨點部分,包括井號 (#)。

使用這些Location對象屬性,您可以訪問所有這些 URL 組件以及它們可以設置或返回的內容:

  • href - 整個網址
  • 協議- URL 的協議
  • host - URL 的主機名和端口
  • 主機名- URL 的主機名
  • port - 服務器用於 URL 的端口號
  • pathname - URL 的路徑名
  • search - URL 的查詢部分
  • hash - URL 的錨點部分
  • origin - window.location.protocol + '//' + window.location.host

我希望你得到你的答案。。

使用window.location對與當前幀關聯的位置對象進行讀寫訪問。 如果您只想將地址作為只讀字符串獲取,則可以使用document.URL ,它應該包含與window.location.href相同的值。

獲取當前頁面 URL:

window.location.href

好的,使用純 JavaScript 可以輕松獲取當前頁面的完整 URL 例如,在此頁面上嘗試以下代碼:

window.location.href;
// use it in the console of this page will return
// http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser"

window.location.href屬性返回當前頁面的 URL。

 document.getElementById("root").innerHTML = "The full URL of this page is:<br>" + window.location.href;
 <!DOCTYPE html> <html> <body> <h2>JavaScript</h2> <h3>The window.location.href</h3> <p id="root"></p> </body> </html>

提一下這些也不錯:

  • 如果您需要相對路徑,只需使用window.location.pathname

  • 如果您想獲取主機名,可以使用window.location.hostname

  • 如果您需要單獨獲取協議,請使用window.location.protocol

    • 另外,如果你的頁面有hash標簽,你可以得到它: window.location.hash

所以window.location.href一次處理所有...基本上:

window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.hash === window.location.href;
    //true

如果已經在窗口范圍內,也不需要使用window ...

因此,在這種情況下,您可以使用:

location.protocol

location.hostname

location.pathname

location.hash

location.href

使用 JavaScript 獲取當前 URL

要獲取路徑,您可以使用:

 console.log('document.location', document.location.href); console.log('location.pathname', window.location.pathname); // Returns path only console.log('location.href', window.location.href); // Returns full URL

打開Developer Tools ,在控制台中輸入以下內容,然后按Enter

window.location

例如:下面是當前頁面的結果截圖。

在此處輸入圖像描述

從這里獲取你需要的東西。 :)

使用: window.location.href

如上所述,更新window.locationdocument.URL不會更新 MDN

  • 使用window.location.href獲取完整的 URL。
  • 使用window.location.pathname獲取離開主機的 URL。

您可以使用以下方法獲取帶有井號標簽的當前 URL 位置

JavaScript:

 // Using href
 var URL = window.location.href;

 // Using path
 var URL = window.location.pathname;

jQuery :

$(location).attr('href');

對於帶有查詢字符串的完整 URL:

document.location.toString()

對於主機 URL:

window.location

// http://127.0.0.1:8000/projects/page/2?name=jake&age=34
let url = new URL(window.location.href);
/*
hash: ""

host: "127.0.0.1:8000"

hostname: "127.0.0.1"

href: "http://127.0.0.1:8000/projects/page/2?username=jake&age=34"

origin: "http://127.0.0.1:8000"

password: ""

pathname: "/projects/page/2"

port: "8000"

protocol: "http:"

search: "?name=jake&age=34"

username: ""
*/

url.searchParams.get('name')
// jake

url.searchParams.get('age')
// 34

url.searchParams.get('gender')
// null
var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
       currentPageUrlIs = this.href.toString().toLowerCase(); 
}else{ 
       currentPageUrlIs = document.location.toString().toLowerCase();
}

上面的代碼也可以幫助某人

添加結果以供快速參考

窗口位置;

 Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
 ancestorOrigins: DOMStringList,
 origin: "https://stackoverflow.com",
 replace: ƒ, assign: ƒ, …}

文件位置

  Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript", 
ancestorOrigins: DOMStringList,
 origin: "https://stackoverflow.com",
 replace: ƒ, assign: ƒ
, …}

窗口.位置.路徑名

"/questions/1034621/get-the-current-url-with-javascript"

window.location.href

"https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript"

位置.主機名

"stackoverflow.com"

對於那些想要一個實際 URL 對象的人,可能是一個將 URL 作為參數的實用程序:

const url = new URL(window.location.href)

https://developer.mozilla.org/en-US/docs/Web/API/URL

Nikhil Agrawal 的回答很棒,只需在此處添加一個小示例,您就可以在控制台中查看不同組件的運行情況:

在此處輸入圖像描述

如果您希望基本 URL 沒有路徑或查詢參數(例如,針對 AJAX 請求在開發/登台和生產服務器上工作), window.location.origin是最好的,因為它保留了協議和可選端口(在Django 開發,有時你有一個非標准端口,如果你只使用主機名等會破壞它。)

獲取當前位置對象的方法是window.location

將此與document.location進行比較,后者最初僅將當前 URL 作為字符串返回。 可能是為了避免混淆, document.location被替換為document.URL

而且,所有現代瀏覽器都將document.location映射到window.location

實際上,為了跨瀏覽器的安全,您應該使用window.location而不是document.location

在 jstl 中,我們可以使用pageContext.request.contextPath訪問當前 URL 路徑。 如果要進行 Ajax 調用,請使用以下 URL。

url = "${pageContext.request.contextPath}" + "/controller/path"

示例:對於頁面http://stackoverflow.com/posts/36577223這將給出http://stackoverflow.com/controller/path

location.origin+location.pathname+location.search+location.hash;

location.href

做同樣的事情。

你有多種方法可以做到這一點。

1:

location.href;

2:

document.URL;

3:

document.documentURI;

用這個:

 var url = window.location.href; console.log(url);

短的

location+''

 let url = location+''; console.log(url);

您可以通過location.href獲取當前頁面的完整鏈接,並獲取當前控制器的鏈接,使用:

location.href.substring(0, location.href.lastIndexOf('/'));

使用 JavaScript 獲取當前 URL:

  • window.location.toString();

  • window.location.href

如果您指的是具有id的特定鏈接,則此代碼可以幫助您。

$(".disapprove").click(function(){
    var id = $(this).attr("id");

    $.ajax({
        url: "<?php echo base_url('index.php/sample/page/"+id+"')?>",
        type: "post",
        success:function()
        {
            alert("The Request has been Disapproved");
            window.location.replace("http://localhost/sample/page/"+id+"");
        }
    });
});

我在這里使用 ajax 提交一個 id 並使用window.location.replace重定向頁面。 只需按照說明添加屬性id=""即可。

要獲取路徑,您可以使用:

http://www.example.com:8082/index.php#tab2?foo=789

Property                    Result
------------------------------------------
window.location.host        www.example.com:8082
window.location.hostname    www.example.com
window.location.port        8082
window.location.protocol    http:
window.location.pathname    index.php
window.location.href        http://www.example.com:8082/index.php#tab2
window.location.hash        #tab2
window.location.search      ?foo=789
window.location.origin      https://example.com

在此處輸入圖像描述

window.location.toString();

window.location.href

window.location.pathname

 let url = new URL(window.location.href); console.log(url.href);

使用上面的代碼獲取網站當前的URL。

或者試試這個 - https://bbbootstrap.com/code/get-current-url-javascript-54628697

首先檢查頁面是否完全加載

browser,window.location.toString();

window.location.href

然后調用一個函數,該函數接受 url、URL 變量並在控制台上打印,

$(window).load(function(){
   var url = window.location.href.toString();
   var URL = document.URL;
   var wayThreeUsingJQuery = $(location).attr('href');
   console.log(url);
   console.log(URL);
   console.log(wayThreeUsingJQuery );
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM