簡體   English   中英

如何獲取JavaScript中當前URL的路徑以用於CodeIgniter?

[英]How can I get the path of the current URL in JavaScript for CodeIgniter usage?

例如: http://localhost/example/testpage/this?page=2

我只想要http://localhost/example/

您想要這樣的東西:

 const url = new URL(window.location.href); console.log(url.protocol + url.hostname + url.pathname); // in your case const testURL = "http://localhost/example/testpage/this?page=2"; const url2 = new URL(testURL); console.log(url2.protocol + url2.hostname + '/' + url2.pathname.split('/')[1]); 

使用下面的代碼行:

window.location.origin+'/'+ window.location.pathname.split('/')[1]

暫無
暫無

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

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