html base的在IE9下无效的问题解决办法

<base> 标签为页面上的全部连接规定默认地址或默认目标。
在页面的head标签内 写上<base href="/org/user/"/> 后,
当前页面中 <a>、<img>、<link>、<form> 标签相对与加上了base的href
例如:
在发送网络请求时,css

<img src="small/icon.jpg">

就变成了html

<img src="/org/user/small/icon.jpg">

在IE十一、Chorme、Firefox下都好好的,到了IE9中,就不行了,哇~~~,而后在网上找了一些资料后,发现
原来在IE9中,base的href必须写为绝对路径,才会有效,如:web

<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浏览器上执行这段jside

var b = document.getElementsByTagName('base')[0];
if(b) b.href=b.href;


下面是我这边完整的页面头部代码ui

<!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>