在动态添加< base>之后加载两次资产标签 [英] assets loaded twice after a dynamically added <base> tag

查看:88
本文介绍了在动态添加< base>之后加载两次资产标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我发现只有一个其他人报告此问题此处



我有一个带有相对路径的html文档。为了简化某些工作,我使用此脚本在文档的头部附加一个基本标记作为第一个子项,其中prd-ita是包含所有网站文件的文件夹:

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>





这样所有相对路径就像newPathname一样(例如http://maxiride.it/bla/bla/ prd-ita 或file:/// D:/ myUser / folder1 / prd-ita )是所有相对路径的前缀。



此方法正常工作,我的内容已正确加载执行和显示。但是,每个资源都加载两次,一次使用newPathname作为前缀(这是正确的),另一次使用实际相对路径(当然给出404错误),就像基本标记不存在一样。



使用Chrome检查元素我看到在加载顺序中,浏览器首次尝试从其相对路径加载资源,并且只有在加载后才考虑基本标记。





我试图将脚本直接放在head标签内部,希望它首先被执行,然后我尝试了这个脚本来自第一行中提到的那个人,我尝试了不同的注射方法来添加花瓶标签(如你所见)来自注释行)



This way all the relative paths acts like if the "newPathname" (eg. http://maxiride.it/bla/bla/prd-ita 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)

推荐答案

(#host)。attr(href,newPathname); //空基础标记,其中id =host放置在头部
// 文档.write('< base id =hosthref ='+ newPathname +'/>'); //脚本内嵌到头部
("#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 =hosthref =' + newPathname + ' />); clping< / body>
( "head" ).prepend('<base id="host" href="' + newPathname + '"/>'); //script before clsing </body>





这样所有相对路径就像newPathname一样(例如http://maxiride.it/bla/bla/ prd-ita 或file:/// D:/ myUser / folder1 / prd-ita )是所有相对路径的前缀。



此方法正常工作,我的内容已正确加载执行和显示。但是,每个资源都加载两次,一次使用newPathname作为前缀(这是正确的),另一次使用实际相对路径(当然给出404错误),就像基本标记不存在一样。



使用Chrome检查元素我看到在加载顺序中,浏览器首次尝试从其相对路径加载资源,并且只有在加载后才考虑基本标记。





我试图将脚本直接放在head标签内部,希望它首先被执行,然后我尝试了这个脚本来自第一行中提到的那个人,我尝试了不同的注射方法来添加花瓶标签(如你所见)来自注释行)



This way all the relative paths acts like if the "newPathname" (eg. http://maxiride.it/bla/bla/prd-ita 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)


这篇关于在动态添加&lt; base&gt;之后加载两次资产标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆