0

I am trying to use PHP to read, then modify and echo an HTML file.

The included HTML file contains external JS, CSS references - all relative paths

for example...

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

Problem : The location of the PHP modifier file is not the same as the location of the included HTML file, and therefore the external includes are not loaded. I guess...

The solution of using absolute paths to reference external resources in the HTML file is not ideal to say the least...

What can be done to tell PHP that the path context of the included HTML file is the same as the directory from which it is being included and NOT the directory of the modifier file?

Thanks!

  • 1
    Maybe add a `/` prior to the path? like `/js/myJavascript.js` and then it would related to the current path. – Ofir Baruch Jun 28 '15 at 05:48
  • 1
    I think you would need an HTML parser to change the DOM of the `src` attributes in your included HTML. – odedta Jun 28 '15 at 06:09
  • @OfirBaruch - tried, not working... thanks! –  Jun 28 '15 at 06:12
  • @odedta - not an ideal solution, further referencing of js, css can occur after the page was loaded... thanks anyway! –  Jun 28 '15 at 06:15
  • 1
    Well, the only solution I can think of is either changing the dom or creating a function that returns the relative path, then, adding that function to all your included HTMLs – odedta Jun 28 '15 at 06:39

1 Answers1

0

Found a solution!

<base href="path_to_the_html" target="_blank">

according to W3Schools : This would specify a default URL and a default target for all links on a page...

  • 1
    @odedta - I found some more info on the outcomes of using this approach here : http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag –  Jun 28 '15 at 06:48