简体   繁体   English

.js 文件适用于 chrome 和 firefox 但不适用于 IE

[英].js file works in chrome and firefox but not IE

When testing my website out in IE11 certain parts of it don't work and I believe the issue lies with my 'custom.js' file as all the problems link to that js.在 IE11 中测试我的网站时,它的某些部分不起作用,我相信问题出在我的“custom.js”文件上,因为所有问题都链接到该 js。 However, the page works perfectly in Chrome and Firefox.但是,该页面在 Chrome 和 Firefox 中完美运行。

I load the .js file called 'custom.js' in the footer of my page along with other page specfic plugins (jquery and bootstrap are loaded in the head) like so:我在页面的页脚中加载名为“custom.js”的 .js 文件以及其他页面特定插件(jquery 和 bootstrap 加载在头部),如下所示:

<!-- JS Implementing Plugins -->
<script type="text/javascript" src="/js/back-to-top.js"></script>
<script type="text/javascript" src="/js/smoothScroll.js"></script>
<script type="text/javascript" src="/js/jquery.parallax.js"></script>
<script type="text/javascript" src="/js/masterslider.min.js"></script>
<script type="text/javascript" src="/js/jquery.easing.min.js"></script>
<script type="text/javascript" src="/js/owl.carousel.min.js"></script>
<script type="text/javascript" src="/js/jquery.cubeportfolio.min.js"></script>

<!-- JS Customization -->
<script type="text/javascript" src="/js/custom.js"></script>

<!-- JS Page Level -->
<script type="text/javascript" src="/js/app.js"></script>
<script type="text/javascript" src="/js/owl-carousel.js"></script>
<script type="text/javascript" src="/js/master-slider-fw.js"></script>
<script type="text/javascript" src="/js/jquery.owl-filter.js"></script>
<script type="text/javascript" src="/js/material.min.js"></script>

<script type="text/javascript">
    jQuery(document).ready(function() {
        App.init();
        App.initCounter();
        App.initParallaxBg();
        FancyBox.initFancybox();
        MSfullWidth.initMSfullWidth();
        OwlCarousel.initOwlEvent();
        OwlCarousel.initOwlSingle();
        OwlCarousel.initOwlTwo();
        OwlCarousel.initOwlAbout();

    });
    $(document).ready(function(){
        $('.owl-carousel').owlCarousel({
            nav:true,
            loop:true
        });
    });

</script>

<!--[if lt IE 9]>
<script src="/plugins/respond.js"></script>
<script src="/plugins/html5shiv.js"></script>
<script src="/plugins/placeholder-IE-fixes.js"></script>

The contents of the custom.js file is: custom.js 文件的内容是:

$(".helpform-container:not(.displayblock)").hide();
    $(".helpform")
        .on('mouseover focus', function(e) {
            $(this).addClass("link-div-hover")
        })
        .on('mouseout blur', function(e) {
            $(this).removeClass("link-div-hover")
        })
        .on('touchstart', function(e) {
            $(this).addClass("link-div-hover")
        })
        .on('touchend', function(e) {
            $(this).removeClass("link-div-hover")
        })
        .on('click', function(e) {
            $(this).toggleClass("active");
            e.preventDefault();

            if ($(".helpform-container").is(":hidden")) {
                $(".helpform-container").slideDown(400).addClass("displayed");
                analyticsevent('How can we help form', 'open');
            } else {
                $(".helpform-container").slideUp(400).removeClass("displayed");
                $("#sticky-wrapper").css("height","auto");
                analyticsevent('How can we help form', 'closed');
            }

            if (sitewidth < 1024) {
                $('html,body').animate({ scrollTop: $("#howcanwehelp").offset().top - 60 }, 250);
            } else {
                $('html,body').delay(500).animate({ 
                    scrollTop: $("#howcanwehelp").offset().top 
                }, 400);
            }
        })

    //FORM METRICS
    if ($('.formsent').length){
         analyticsevent('Contact form completed', 'consultation/quote/info/media');
    }

//Homepage news articles

var divs = $(".owl-news > .news-v2");
let array = [
  { length: 1, num: 4 },
  { length: 2, num: 3 },
  { length: 2, num: 3 },
  { length: 3, num: 2 }  
];

let i = 0;


for (let item of array) {
  divs.slice(i, i+item.length).wrapAll(`<div id='news-${item.num}' class='col-md-${item.num}'></div>`);
  i += item.length;
}

$("#news-4").before("<div class='col-md-4'><h3 id='title_featured'>Featured News</h3></div><div class='col-md-8'><h3 id='title_latest'>Latest News</h3></div>");

Problem: Internet Explorer 11, released in 2013, does not run ECMAScript 2015 (for obvious reason).问题:2013 年发布的 Internet Explorer 11 不运行 ECMAScript 2015(原因很明显)。

Dirty Way: Babel (Standalone)肮脏的方式:巴别塔(独立)

The quickest but also the least efficient way.最快但也是效率最低的方式。 Don't use in production.不要在生产中使用。

<!-- Load the in-browser babel compiler.  Make sure page encoding is UTF-8. -->
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<!-- Set script type to text/babel for on-the-fly conversion and execution -->
<script type="text/babel" src="custom.js"></script>
<!-- Babel need to read the script through ajax, same origin policy applies. -->

Painful Way: Rewrite in ES5痛苦的方式:用 ES5 重写

Simply rewrite the last few lines of custom.js in ES5, and be extra care not to use any ES6/7/8+ features in the future:只需在 ES5 中重写custom.js的最后几行,并且以后要格外小心不要使用任何 ES6/7/8+ 特性:

var divs = $(".owl-news > .news-v2"),
   array = [
      { length: 1, num: 4 },
      { length: 2, num: 3 },
      { length: 2, num: 3 },
      { length: 3, num: 2 }  
   ],
   i = 0;

array.forEach( function( item ) {
  divs.slice(i, i+item.length).wrapAll( "<div id='news-"+item.num+"' class='col-md-"+item.num+"'></div>" );
  i += item.length;
} );

Systematic Way: Build Script系统方式:构建脚本

A proper build system can help you manage the project, such as automatic testing and deployment to test and production system.合适的构建系统可以帮助您管理项目,例如自动测试和部署到测试和生产系统。 One of the things they can do is to convert your ES6 code to ES5 on deployment and maybe minify / obfuscate them, for example with Babel ,Traceur , or Closure .他们可以做的一件事是在部署时将您的 ES6 代码转换为 ES5,并可能缩小/混淆它们,例如使用BabelTraceurClosure

The "build system" can be as simple as a batch file. “构建系统”可以像批处理文件一样简单。 If you tell your boss it'll protect valuable company intellectual properties (s)he may give you the time you need to properly learn one.如果你告诉你的老板它会保护有价值的公司知识产权,他可能会给你足够的时间来正确学习。


Save the Web: Don't support IE 11拯救网络:不支持 IE 11

I know, I know.我知道我知道。 You won't be asking if it's an option.你不会问它是否是一种选择。

But your boss may not be aware that IE support costs more development time, which means higher cost, slower delivery, and less profit.但是你的老板可能没有意识到 IE 支持会花费更多的开发时间,这意味着更高的成本、更慢的交付和更少的利润。

Few people (3.2%) use IE 11 in the real world - less than "UC Browser" (8%), "Firefox" (6%), "Samsung Internet" (3.6%), or "Opera" (3.4%).很少有人 (3.2%) 在现实世界中使用 IE 11——少于“UC 浏览器”(8%)、“Firefox”(6%)、“三星互联网”(3.6%) 或“Opera”(3.4%) . (Statcounter May 2017 global stat .) If a user or client ask why don't you support the Samsung browser , saying no one use it is not a good excuse, as IE 11 has even less users. (Statcounter的2017年5月全球统计)。如果一个用户或客户问你为什么不支持三星的浏览器,他说没有人使用它不是一个很好的借口,因为IE 11甚至更少的用户。

Most IE users have learned that if it doesn't work in IE, try Chrome.大多数 IE 用户都了解到,如果它在 IE 中不起作用,请尝试使用 Chrome。 Encourage them to use Chrome first and IE last will be good.鼓励他们先使用 Chrome,最后使用 IE 会更好。 They are safer, the web is brighter, everyone is happy.他们更安全,网络更明亮,每个人都很开心。

You're using the javascript let keyword, which is only available on IE11.您正在使用 javascript let关键字,该关键字仅在 IE11 上可用。 More information here ...更多信息在这里...

If the problem persists and you are sure the code is compiling, then you should activate the browser's debugger to figure out what's going on... Just place the keyword debugger;如果问题仍然存在并且您确定代码正在编译,那么您应该激活浏览器的调试器以找出发生了什么......只需放置关键字debugger; somewhere in your code, and the browser will suspend execution at that stop allowing you to inspect the variables...在您的代码中的某个地方,浏览器将在该停止处暂停执行,允许您检查变量...

Something like this:像这样的东西:

debugger;
$(".helpform-container:not(.displayblock)").hide();
...
$("#news-4").before("<div class='col-md-4'><h3 id='title_featured'>Featured News</h3></div><div class='col-md-8'><h3 id='title_latest'>Latest News</h3></div>");

Easy way to check if your file has non-es5 syntax:检查您的文件是否具有非 es5 语法的简单方法:

npm install -g es-check
es-check es5 offendingFile.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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