Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewriting Root Directory Path "/" for Javascript

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

like image 962
Starx Avatar asked Oct 10 '22 17:10

Starx


1 Answers

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.

like image 86
Stein G. Strindhaug Avatar answered Nov 03 '22 19:11

Stein G. Strindhaug