Using $_SERVER['DOCUMENT_ROOT'] to include JS files

Hi folks,

i am using $_SERVER[‘DOCUMENT_ROOT’] to include a js file. The file path prints correctly when viewed via ‘view source’. but it is 404. whats the problem ?

echo "<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"" . $_SERVER['DOCUMENT_ROOT'] . "/js/load_select_ajax.js\"></SCRIPT>";

out put :

<SCRIPT LANGUAGE="JavaScript" SRC="/home4/hrsjedco/public_html/visa/js/load_select_ajax.js"></SCRIPT>

Can I ask what you’re expecting to happen? My PHP knowledge is limited, but I’m reading your question as if you’re expecting a .js file to show up as if it were a webpage file. Is that correct, or are you expecting the .js to be embedded within something larger. What does the rest of the ‘view source’ look like?

1 Like

Your starting point is above the public part of the site and files referenced from within the HTML must have either an absolute address or an address relative to the HTML.

You need to get rid of the part of the address quoted above.

1 Like

Because this is a path in your file system, not the web URL.
Your “path” (actually it is URL) should be like:

<script type="text/javascript" src=""></script>
1 Like

Thanks folks,

as you specified, i have now removed the $_SERVER[‘DOCUMENT_ROOT’] part and it works.

<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"/js/load_select_ajax.js\"></SCRIPT>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.