最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

path - javascript site root - Stack Overflow

programmeradmin1浏览0评论

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/...

LIVE SERVER TEST FOLDER: www.asite/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.

LOCAL SERVER: localhost/mytestSiteA/subsection/...

LIVE SERVER TEST FOLDER: www.asite/devbuild/subsection/...

I hope I have made this as easy to understand and put across. Si

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/...

LIVE SERVER TEST FOLDER: 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.

LOCAL SERVER: localhost/mytestSiteA/subsection/...

LIVE SERVER TEST FOLDER: www.asite.com/devbuild/subsection/...

I hope I have made this as easy to understand and put across. Si

Share Improve this question asked Sep 4, 2012 at 8:54 Simon DaviesSimon Davies 3,6869 gold badges44 silver badges69 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 21

try to 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;

This is what worked for me after the switch clause.

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

if someone needs to move a step back

location.href.slice(0,location.href.lastIndexOf("/"))

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.

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

发布评论

评论列表(0)

  1. 暂无评论