2013-05-29 73 views
1

我如何才能在我的網站上的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

+0

你找'window.location.href' – bipen

+0

的可能的複製http://stackoverflow.com/questions/2255689/how-to-get-the-file-path-of - 當前正在執行的JavaScript代碼 – PLPeeters

回答

0

你可以使用document.location.hostname返回您的主機名。例如,如果你要在SO上運行它,它的值將是stackoverflow.com

1

window.location.origin將提供URL的域部分。然後你可以追加你的路徑。

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

例如:

// Prints "http://stackoverflow.com" 
console.log(window.location.origin); 
+0

我需要確切的路徑下面的級別:http://example.com/wp-content/themes/example/js/這是因爲主題名稱可以改變。 – JohnSmith

+0

主題名稱來自哪裏?你可以把它傳遞給Javascript文件。 – CodingIntrigue