1

In a php class, assuming the js file is in the same directory, but I can never predict where the two files will be located on the server, how do I make sure the js file is properly referenced?

This doesn't work at all:

    function import_script() {
    ?>
        <script src="<?php echo(__DIR__); ?>\myscriptfile.js"></script>

I hope this is clear enough - enough code foryou to see what I'm trying to do. Feel free to request more and I'll edit the question.

2
  • You need to know locations of js file, dont do dir__, give path /pathtojs/jsfile.js relative to web document root Commented Mar 4, 2016 at 17:19
  • please elaborate more Commented Mar 4, 2016 at 17:23

3 Answers 3

0

You can even try this

function import_script() {
    echo "<script src='".__DIR__."/myscriptfile.js'></script>";
}

For more reference check this SO question how to write HTML inside PHP

Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to do any of the fancy coding just put a . before the first slash like this:

<script src="./myscriptfile.js"></script>

When getting directory listings, you notice there is . and .., . indicates current directory, while .. indicates the parent directory. All without having to know the current directories name, so you could change it every hour, the script would work then same.

Comments

0

This is a simple question with a vast answers. Keeping your assets in relative directory or absolute, might require some knowledge on your framework, but simply:

function import_script() {
?>
    <script src="myscriptfile.js"></script>

if you are pretty sure it's located same directory as your php url file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.