今天在編寫代碼的時候遇到了一個莫名其妙的問題,請看下面
<html>
<head>
<title> Test </title>
<!--<meta http-equiv="X-UA-Compatible" content="IE=8">-->
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
</head>
<body>
<script type="text/javascript">
var json = "{\"title\":\"測試\"}"
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
var ieversion=new Number(RegExp.$1)
alert("IE"+ieversion);
try
{
json = JSON.parse(json);
alert(json.title);
}
catch (e)
{
alert(e.name + " : " + e.message);
}
}else
{
json = JSON.parse(json);
alert(json.title);
}
</script>
</body>
</html>
以上代碼若使用IE8打開,默認是會提示錯誤的,若將meta中註釋掉的地方放開,則不會報錯。
爲什麼是這樣呢?這是因爲在瀏覽器中有個叫模式和標準的東西,IE8模式IE7標準,在此IE版本檢測中提示爲IE8,但是卻缺少IE8特有的屬性和函數,例如JSON函數。
這就是爲什麼加上meta就不報錯,去掉就會報錯了。
這就有問題了,大多數人都認爲IE8是支持JSON的,但實際上,只有IE8模式IE8標準才支持JSON!
對了,也可以用 <meta http-equiv=”X-UA-Compatible” content=”IE=edge” >這個來設置IE默認使用最新IE解析