Skip to content
Advertisement

Include external javascript file in header navbar file not found

I have the following folder structure:

  • website.com/index.php
  • website.com/page/asd.php
  • website.com/header.php
  • website.com/js/file.js

header.php contains:

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

index.php contains:

include_once 'header.php';

asd.php contains:

include_once  $_SERVER['DOCUMENT_ROOT'] . '/header.php';

So file.js will work on index.php, but file.js doesnt work on asd.php. On asd.php the browser want to get /pages/js/file.js (which doesnt exist) and not /js/file.js

How can I handle this problem? Thank you.

Advertisement

Answer

Start path of your file with /

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

This will tell browser to look from the root directory.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement