简体   繁体   中英

Insert JavaScript file if the IE document.mode < 8

I have the following script within the head of my ASP.Net page. This will only include script file if the IE version is less than 8, but if the IE version is 10 and document mode is 7 then this script will not get included.

Is there a way to include dynamically javascript file based on document.mode value using similar notation?

<!--[if lt IE 8]>
  <script src="xyz.js">
<![endif]-->

EDIT 1: the following gave unterminated string error in IE 10.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title></title>
  <script>/*<![CDATA[*/if(document.documentMode==7)document.write("<script src=\"xyz.js">
    </script>");/*]]>*/</script>
 </head>

Dynamically Include a javascript file

It's not an IE conditional comment, but it will dynamically include a JavaScript file based on the documentMode and will not affect other browsers. Choose which one you like best:

<script>/*<!--*/if(document.documentMode==7)document.write('<script src="xyz.js"><\/script>');/*-->*/</script>

<script>/*<![CDATA[*/if(document.documentMode==7)document.write('<script src="xyz.js"><\/script>');/*]]>*/</script>

<script>if(document.documentMode==7)document.write('<script src="xyz.js"><\/script>');</script>

Thanks @Jonathan Lonowski for the last option!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM