2013-05-29 68 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