2014-09-28 28 views
2

我想仅包含一个用于IE8浏览器的JavaScript文件。例如:如何在meteor.js包中包含用于IE8浏览器的JavaScript文件?

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

我使用我的流星包继package.js代码:

Package.onUse(function (api) { 
    api.versionsFrom("[email protected]"); 
    api.use('jquery'); 
    var path = Npm.require('path'); 
    var asset_path = path.join('js); 
    // Surround following code in <!--[if lt IE 9]> and <![endif]--> tags somehow!!!! 
    api.addFiles(path.join(asset_path, 'ie', 'html5shiv.js'), 'client'); 
} 

我知道,最终方案将包含所有js文件组合在一个js文件的缩小版本。在这种情况下,我想有代码的样子:

<!--[if lt IE 9]> 
    // Contents of html5shiv.js here! 
<![endif]--> 

回答

1

这里是我提出解决方案,可惜我不能测试它,因为我可以在此刻没有Windows环境。

my-package/client/views/lib/head.html

<head> 
    <!--[if lt IE 9]> 
     <meta name="lower-than-ie9"> 
    <![endif]--> 
</head> 

head和流星Spacebars模板语法body标签被特殊处理:无论你这些标签之间放(它们可以出现多次)将获得附加到最终的HTML文档。

这就是为什么我们这个流星模板的代码添加到产品包:如果IE版本比IE9是低,它最终将创建一个meta标签名称为lower-than-ie9,我们可以在以后的测试存在或不存在。

my-package/client/lib/my-package.js

Meteor.startup(function(){ 
    // search for the meta tag appended by the conditional inclusion comment 
    if($("meta[name='lower-than-ie9']").length){ 
    // if it's there, load the script with jQuery 
    $.getScript("/my-package/public/html5shiv.js"); 
    } 
}); 

你需要把html5shiv.js脚本在你的包的公共目录:

my-package/public/html5shiv.js

这是包,你将需要结构:

my-package/package.js

Package.onUse(function(api){ 
    api.versionsFrom("[email protected]"); 
    api.use("jquery","client"); 
    api.addFiles([ 
    "client/views/lib/head.html", 
    "client/lib/my-package.js" 
    ],"client"); 
    // specify that the script is an asset to prevent it from 
    // being minimized and bundled to the client 
    api.addFiles("public/html5shiv.js","client",{ 
    isAsset:true 
    }); 
}); 
+0

有趣的解决方案!我在想如果这可以做到不污染Meteor.startup func。希望我的软件包能够开箱即用。也许我应该编辑html5shiv.js,并将其所有内容放在“”“if(!document.addEventListener){//html5shiv.js content here!}”“”中。你怎么看? – 2014-09-29 16:26:55

+0

为什么不把'script'标签直接放在头部,在条件内? – Gigo 2015-12-03 23:57:54

相关问题