Jump to content

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


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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.