Javascript javascript站点根目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12259958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript site root
提问by Simon Davies
I have this site that I need to find the root folder / plus the actual folder its works out of.
我有这个网站,我需要找到根文件夹/加上它工作的实际文件夹。
My problem here is that during development i have the folder with in my local server that in turn is with in its own folder:
我的问题是,在开发过程中,我在本地服务器中有一个文件夹,而该文件夹又在它自己的文件夹中:
Then online I then have the development site within a folder, so it can all be tested before the live production etc.
然后在线我将开发站点放在一个文件夹中,因此可以在现场制作等之前对其进行测试。
LOCAL SERVER: localhost/mytestSiteA/...
本地服务器:本地主机/mytestSiteA/...
LIVE SERVER TEST FOLDER: www.asite.com/devbuild/....
实时服务器测试文件夹:www.asite.com/devbuild/....
Now I can retrieve the root via the
现在我可以通过
document.location.hostname
But i need then to add the folder name after this so that I can load in content etc when in developement mode.
但是我需要在此之后添加文件夹名称,以便在开发模式下加载内容等。
LOCAL SERVER
本地服务器
document.location.hostname + '/mytestSiteA/'
LIVE TEST SITE
现场测试现场
document.location.hostname + '/devbuild/'
But my issue is, is there an easy way to gain this inner folder rather than setting up variables determined on whether in local dev, live dev or live mode, as can be a pain, and would be nice to gain the current inner folder dynamically rather that manually changing etc so that I can add my paths correctly.
但我的问题是,是否有一种简单的方法来获取这个内部文件夹,而不是设置由本地开发、实时开发或实时模式决定的变量,这可能会很痛苦,并且动态获取当前内部文件夹会很好而不是手动更改等,以便我可以正确添加我的路径。
Also would help as if I have a folder within this that also loads in js script it can obtain its full path.
也有帮助,好像我在其中有一个文件夹,它也加载在 js 脚本中,它可以获得其完整路径。
LOCAL SERVER: localhost/mytestSiteA/subsection/...
本地服务器:localhost/mytestSiteA/subsection/...
LIVE SERVER TEST FOLDER: www.asite.com/devbuild/subsection/...
实时服务器测试文件夹:www.asite.com/devbuild/subsection/...
I hope I have made this as easy to understand and put across. Si
我希望我已经使这变得容易理解和表达。硅
回答by diEcho
回答by user2905032
This is what worked for me after the switch
clause.
这就是switch
条款之后对我有用的东西。
var root = location.protocol + '//' + location.host + rootFolder;
回答by Asciiom
You can map the url localhost/devbuild to localhost/mytestSiteA and use the first url to test your site locally. In your javascript you can always assume the devbuild folder then. That way you don't have to change anything else.
您可以将 url localhost/devbuild 映射到 localhost/mytestSiteA 并使用第一个 url 在本地测试您的站点。在您的 javascript 中,您始终可以假设 devbuild 文件夹。这样您就不必更改任何其他内容。
回答by Dan Mihail Matei
use relative paths so you don't need to get to the root folder
使用相对路径,因此您无需进入根文件夹
this doesn't work on sites with friendly urls with folders in the links
这不适用于链接中带有文件夹的友好网址的网站