Rewriting the root path of the "/" directory for Javascript

We can change the document root path for PHP using

$_SERVER['DOCUMENT_ROOT'] = "to/some/new/directory";
//Now the "/" which represent the ^(above) path

in .htaccess we have

RewriteBase "/to/some/new/directory"

Now I need to change the root path of the directory to use in javascript. How to do it?

I am currently declaring a variable containing a static path to my personal root directory and use it as

var root = "../to/new/path";
document.location = root+"/somepage.php";

Scenario

I think I should tell you a little about the script, because you guys will catch my idea

Default web root directory

http_docs/

inside it contains the main folder

http_docs/application <-- contains the actual application
http_docs/js <-- contains the script
http_docs/index.html

Now the application also contains an ajax function for updating, editing, downloading new content or other resources, which, if access to "/" will be displayed in / some / path / i /, are called not / application / some / path / i / is called,

var root = "application/";

-

$.post(....., function(data) { $(body).append("<img src='"+root+"resources/img1.jpg"); });

, , , , , , js-. , , , PHP, <img src="/resources/img1.jpg" />

, , , , . , .

+5
2

EDITED: , JavaScript index.html, img URL-, . <img src='application/resources/img1.jpg'> . script (, /etc/etc 2/somePage.html "application/resources/" ), URL-, javascript , php, URL- "root" , :

<!-- included by php in all html pages, e.g. in defautlHeadter.php -->
<script type="text/javascript">
   var rootUrl = "<?= getTheRootUrl() ?>";
</script>

getTheRootUrl() - , URL. URL- ( apache .. php), URL- php, , - .

url /- rootUrl + "/some/relative/path" .

+5

- ,

window.app_absolute = '<?php echo GetRelativePath(dirname(__FILE__)); ?>'

-

static function GetRelativePath($path)
{
    $dr = $_SERVER['DOCUMENT_ROOT']; //Probably Apache situated

    if (empty($dr)) //Probably IIS situated
    {
        //Get the document root from the translated path.
        $pt = str_replace('\\\\', '/', Server::GetVar('PATH_TRANSLATED',
            Server::GetVar('ORIG_PATH_TRANSLATED')));
        $dr = substr($pt, 0, -strlen(Server::GetVar('SCRIPT_NAME')));
    }

    $dr = str_replace('\\\\', '/', $dr);

    return substr(str_replace('\\', '/', str_replace('\\\\', '/', $path)), strlen($dr));
}

... - , .

-1

All Articles