Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everybody, I found only one other person reporting this issue here.

I have an html document with relative paths in it. To ease some work I append a base tag as a first child in the head of the document using this script where prd-ita is the folder containing all the website files:
/* Where am I? */
here = window.location.href;
hereIndex = here.indexOf('prd-ita');

/* make substring from root till prd-ita/ */
newPathname = here.substring(0, hereIndex+8); //+8 to consdier also prd-ita/

// $("#host").attr("href", newPathname); // empty base tag with id="host" placed placed in head
// document.write('<base id="host" href="' +  newPathname  + '"/>'); //script inline into the head
$( "head" ).prepend('<base id="host" href="' +  newPathname  + '"/>'); //script before clsing </body>

This way all the relative paths acts like if the "newPathname" (eg. or file:///D:/myUser/folder1/prd-ita) is the prefix for all relative paths.

This method works properly and my content is properly loaded executed and shown. However every resource is loaded twice, once with the newPathname as a prefix (and this is correct) and another time with the real relative path (giving of course a 404 error) like if the base tag was absent.

Using Chrome inspect element I saw that in the loading order the browser first attempt was to load the resources from their relative paths, and only after it loaded them considering the base tag.

I tried to place the script inline directly inside the head tags hoping it would be executed for first, then I tried the this script from the guy mentioned in the first line, I tried different "injections" method to add the vase tag (as you can see from the commented lines)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900