简体   繁体   English

javascript网站根目录

[英]javascript site root

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/... 本地服务器:localhost / 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

try to switch 尝试switch

switch (document.location.hostname)
{
        case 'asite.com':
                          var rootFolder = '/devbuild/'; break;
        case 'localhost' :
                          var rootFolder = '/mytestSiteA/'; break;
        default :  // set whatever you want
}

and then use 然后使用

var root = document.location.hostname + rootFolder;

这是在switch子句之后为我工作的。

var root = location.protocol + '//' + location.host + rootFolder;

You can map the url localhost/devbuild to localhost/mytestSiteA and use the first url to test your site locally. 您可以将URL localhost / devbuild映射到localhost / mytestSiteA并使用第一个URL在本地测试您的站点。 In your javascript you can always assume the devbuild folder then. 在您的JavaScript中,您始终可以假定devbuild文件夹。 That way you don't have to change anything else. 这样,您无需更改其他任何内容。

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 这不适用于链接中带有文件夹的友好网址的网站

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM