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

Rewriting Root Directory Path "" for Javascript - Stack Overflow

programmeradmin1浏览0评论

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+"/somepage.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 e 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 munity, lets help them understand. I wele you to edit my question.

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+"/somepage.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 e 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 munity, lets help them understand. I wele you to edit my question.

Share Improve this question edited Jun 21, 2011 at 9:26 Starx asked Jun 21, 2011 at 8:33 StarxStarx 79.1k50 gold badges186 silver badges265 bronze badges 12
  • 4 What is the question? What do you mean by "modify the root directory"? – Stein G. Strindhaug Commented Jun 21, 2011 at 8:40
  • Both of them deal on the Server-Side. So whats the deal you want to settle with JS? – Sujit Agarwal Commented Jun 21, 2011 at 8:43
  • @Stein G. Strindhaug, I dont know how much more clear than telling you that I want to the modify root directory path for use in javascript – Starx Commented Jun 21, 2011 at 8:49
  • And what is a "root directory path" in JavaScript? – Jan Hančič Commented Jun 21, 2011 at 8:55
  • @Starx why do you want to change the url for the client. Why not have it just be example./ your other relative urls should just work. – Raynos Commented Jun 21, 2011 at 8:56
 |  Show 7 more ments

2 Answers 2

Reset to default 5

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)

  1. 暂无评论