Jump to content

Can I set Javascript's base href (base URL) for all relative links?


MrCat

Recommended Posts

I'm using <base href> to set my HTML path for links, and chdir() to set PHP's base path.

(Entering site at www.mysite.com/account then resetting my paths back to root for simplicity).

 

Both of these are working fine, however I've found Javascript ignores <base href> so it's working directory is still a subfolder of root.

OK - so I could add '../' to all Javascript links (which is what I was trying to get away from!) but I've read that it's only IE that ignores <base href> and other browsers accept it.

This will mean my links break unless I either stop using <base href> entirely and go back to using '../' in ALL links, or I can find a way to set Javascript's base link pointer...

 

This is driving me crazy! Any ideas of how to do it?

 

Generally what I do is in the head of my document, prior to loading any JavaScript files I'll create a JavaScript variable 'g_url' and set this to my sites base URL, which is set via PHP.

 

<script type="text/javascript">
var g_url = 'http://www.yourbaseurl.com/';
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.