Vote Up 0 Vote Down

javascript include relative path

I need to include javascript filenames in a document. But when I include it I ghet relative path issue like following:

<script type="text/javascript" src="folder1/myscript.js"></script>


But it only works in the parent folder. When I went inside the file inside the folder directory I get
404 file not found error in the firebug.

How to include the javascript files such that it works perfectly wherever it is included.
flag

Answers


Vote Up 0 Vote Down
You can use absolute positioning with the help of a server side language like php. From php you can identify the document root
so that you can place it before the source file url like the following code :

<?php

$document_root = 'http://example.com';
?>


And then use

 <script type="text/javascript" 
src="<?php echo $document_root; ?>/folder1/myscript.js"></script>


By the help of this script you can include the file wherever you want without worrying about which folder you were in.
flag | link |

Your Answer

Login before answering

Login with facebook