How to include a remote JavaScript file conditiona

2019-01-14 15:33发布

I have a HTML page.

In that, according to the browser, I need to include a separate JavaScript file.

How is it possible?

<script type="text/javascript">
if(navigator.appName == 'Microsoft Internet Explorer')
{
//here i need to include one.js
}
else
{
//here i need to include two.js
}

</script>

8条回答
在下西门庆
2楼-- · 2019-01-14 16:33

And finally, if you're already using JQuery in your project and just left out the tag for it, you can use $.getScript

查看更多
你好瞎i
3楼-- · 2019-01-14 16:35

I recommend you to use LAB.js or YepNope (script loaders). Both make great effort on loading external scripts the best way possible.

For an example, using YepNope, with two conditional loads:

var agent = navigator.userAgent;
yepnope({
    test : /(msie) ([\w.]+)/.test(agent), // internet explorer
    yep  : 'ie.js',
    nope : 'other-script-if-you-want.js'
});
yepnope({
    test : /(mozilla)(?:.*? rv:([\w.]+))?/.test(agent), // firefox
    yep  : 'firefox.js'
});
查看更多
登录 后发表回答