我如何才能在我的網站上的functions.js文件中獲得functions.js文件的完整路徑?獲取JS文件的URL
例子:我的JS文件是在以下文件夾 /wp-content/themes/example/js/functions.js
我通過jQuery要拉的完整路徑: http://example.com/wp-content/themes/example/js/functions.js
我如何才能在我的網站上的functions.js文件中獲得functions.js文件的完整路徑?獲取JS文件的URL
例子:我的JS文件是在以下文件夾 /wp-content/themes/example/js/functions.js
我通過jQuery要拉的完整路徑: http://example.com/wp-content/themes/example/js/functions.js
你可以使用document.location.hostname
返回您的主機名。例如,如果你要在SO上運行它,它的值將是stackoverflow.com
。
window.location.origin
將提供URL的域部分。然後你可以追加你的路徑。
https://developer.mozilla.org/en-US/docs/Web/API/window.location
例如:
// Prints "http://stackoverflow.com"
console.log(window.location.origin);
我需要確切的路徑下面的級別:http://example.com/wp-content/themes/example/js/這是因爲主題名稱可以改變。 – JohnSmith
主題名稱來自哪裏?你可以把它傳遞給Javascript文件。 – CodingIntrigue
你找'window.location.href' – bipen
的可能的複製http://stackoverflow.com/questions/2255689/how-to-get-the-file-path-of - 當前正在執行的JavaScript代碼 – PLPeeters