Javascript site root

I have this site that I need to find the root folder / plus the actual folder from which it works.

My problem is that during development, I have a folder on my local server, which, in turn, is in its own folder:

Then on the Internet I have a development site in a folder, so it can be tested before live execution, etc.

LOCAL SERVER: local / mytestSiteA / ...

TEST PASSPORT LIVE SERVER: www.asite.com/devbuild / ....

Now I can get the root with

    document.location.hostname 

But I need to then add the folder name after this so that I can upload content, etc. in development mode.

LOCAL SERVER

 document.location.hostname + '/mytestSiteA/'

LIVE TEST SITE

 document.location.hostname + '/devbuild/'

, , , dev, live dev live , , , , .., .

, , js script, .

: /mytestSiteA//...

: www.asite.com/devbuild/subsection/...

, . Si

+5
4

switch

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

var root = document.location.hostname + rootFolder;
+13

, switch.

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

You can map the localhost / devbuild url to localhost / mytestSiteA and use the first URL to test your site locally. In your javascript, you can always accept the devbuild folder. This way you do not need to change anything.

0
source

use relative paths so you don't have to get to the root folder

this does not work on sites with friendly urls with folders in links

-1
source

All Articles