简体   繁体   中英

How to write advanced conditions in RequireJS?

How to write custom conditions in including JavaScript files using RequireJS?

Example:1

    if("ontouchend" in document) document.write("<script
    src='/js/jquery.mobile.custom.min.js'>"+"<"+"/script>");

Example:2

    <!--[if lt IE 9]>
    <script src="/js/html5shiv.js"></script>
    <![endif]-->

Currently I am using shim and paths. Is there any room to place this. Is it possible?

You could use a conditional specific class on the html element itself, then checking for it and loading the dependencies will be easier in the inside your existing script.

For Example:

<!--[if lt IE 7]> <html class="ie lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>    <html class="ie lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>    <html class="ie lt-ie9"> <![endif]-->
<!--[if gt IE 8]> <html class="ie"> <![endif]-->
<html>

And in your script, you could try something like:

if ($('html.lt-ie9').size()) {
    require(['/Scripts/ieSpecificScripts'], function(ieScript) {
        // ... do stuff
    });
}else
{
...
}

Happy Coding.. :)

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