开发者

Rewriting Root Directory Path "/" for Javascript

开发者 https://www.devze.com 2023-03-14 13:24 出处:网络
We can modify the document root directory path for PHP using $_SERVER[\'DOCUMENT_ROOT\'] = \"to/some/new/directory\";

We can modify the document root directory 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 modify the root directory path to use in javascript. How to do it?

Currently, I am declaring a variable containing static path to the my personalized root directory and using it as

var root = "../to/new/path";
document.location = root+"/some开发者_开发百科page.php";

Scenario

I think i should tell a little bit about the scenario, for you guys to catch my idea

Default Web Root Directory

http_docs/

inside it contain a main folder

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

Now, the application also contains ajax feature for updating, editing, loading new content, or other resources, which if accessed at "/" will represent at /some/path/i/called not /application/some/path/i/called,

To come around this problem I can define a static variable like

var root = "application/";

and use it somewhere like

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

But for a single use, defining the path as static, might not be a big deal, but, when the application grows, and certain modification would cause me to change all the paths i give in the js part. I thought, it would be sensible, just like, I do it in PHP, using <img src="/resources/img1.jpg" />

I tried my best to explain this question, if still is not understandable, please community, lets help them understand. I welcome you to edit my question.


EDITED: Trying to answer the updated question Assuming the JavaScript is called included from the index.html file, if you insert a img tag and use relative urls, they will be relative to the path of the index file. So <img src='application/resources/img1.jpg'> would work just fine. If the script should work for several sublevels (e.g. if the page "application/etc/etc2/somePage.html" needs images from "application/resources/")it may be easier to use absolute urls, and you could include a javascript block on every page generated by php that holds the absolute url to the "root" of the application, like:

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

Where getTheRootUrl() is a method or server variable that gives the root url you need. If the url is translated/remapped (by apache etc. outside of what is visible to php) you may need to hardcode the root url in the php method but at least it will be only one file to change if you ever change the root directory.

Then you can use the root url to specify absolute paths anywhere in the application/website using rootUrl + "/some/relative/path" in anywhere in the application.


I once made something like this, to set

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

I also use something like this

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));
}

... Something along those lines, hacked up for demonstration purposes.

0

精彩评论

暂无评论...
验证码 换一张
取 消