Is there a way to introduce Internet Explorer conditional comments using JavaScript?

不羁岁月 提交于 2019-12-06 11:15:39

You can use conditional comments inside innerHTML as follows:

var div = document.createElement("div");
div.innerHTML = "<!--[if IE]><i></i><![endif]-->";
if (div.getElementsByTagName("i").length) {
    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = "ie_only.css";
    document.getElementsByTagName("head")[0].appendChild(link);
}

Very simple:

/*@cc_on document.createStyleSheet("ie.css"); @*/
Eduardo

Do you know Modernizr?

Also, it is great to use this in the HTML open tag (replace the no-js with js!)

<!--[if lt IE 7 ]><html lang=en-us class="no-js ie6"><![endif]--> 
<!--[if IE 7 ]><html lang=en-us class="no-js ie7"><![endif]--> 
<!--[if IE 8 ]><html lang=en-us class="no-js ie8"><![endif]--> 
<!--[if (gte IE 9)|!(IE)]><!--> <html lang=en-us class=no-js> <!--<![endif]--> 

So you can do this in CSS:

.ie7 .myDiv {
    foo:bar;
}

Can also be accessed in javascript with:

if( document.body.className.indexOf("ie") > -1 ){
     //
}

// or using jQuery

if( $(document.body).hasClass("ie6") ){
}

All this is unnecessary.

public void renderHead(IHeaderResponse response) if (WebSession.get().getClientInfo().getClientProperties().isBrowserInternetExplorer()) { response.renderJavaScriptReference(new JavaScriptResourceReference(MyClass.class, "ie.js")); }

If you choose to use Modernizr be aware of https://issues.apache.org/jira/browse/WICKET-3433

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!