html base的在IE9下无效的问题解决办法
<base> 标签为页面上的所有链接规定默认地址或默认目标。
在页面的head标签内 写上<base href="/org/user/"/> 后,
当前页面中 <a>、<img>、<link>、<form> 标签相对与加上了base的href
例如:
在发送网络请求时,
<img src="small/icon.jpg">
就变成了
<img src="/org/user/small/icon.jpg">
在IE11、Chorme、Firefox下都好好的,到了IE9中,就不行了,哇~~~,然后在网上找了一些资料后,发现
原来在IE9中,base的href必须写为绝对路径,才会有效,如:
<base href="https://blog.51cto/com/org/user/"/>
所以我在页面中,我使用js动态的给base的href赋值。本来打算这样写
var b = document.getElementsByTagName('base')[0];b.href = location.protocol+"//"+location.host+b.href;
但是发现通过js拿到的href属性值就已经是绝对路径了
b.href = "https://blog.51cto/com/org/user/"
所以,我就这样写了,加个IE才能识别的标签,等于在 ≤ IE9 版本的IE浏览器上执行这段js
var b = document.getElementsByTagName('base')[0];if(b) b.href=b.href;
。
下面是我这边完整的页面头部代码
<!DOCTYPE html><html lang="zh-Hans"><head> <meta charset="UTF-8"> <title>51CTO</title> <base href="/org/user/"><!--[if lte IE 9]> <script>!function(){var b = document.getElementsByTagName('base')[0];if(b)b.href=b.href;}()</script><![endif]--> <meta http-equiv="X-UA-Compatible" content="IE=edge,chorme=1"> <meta name="renderer" content="webkit"> <link rel="stylesheet" id="hint-css" href="/static/css/hint.min.css"></head>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。