3

I am using chrome latest version. My application throws error:

Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/6662647093133312

It hinders it ability to properly scroll i.e. jerky scroll. I tried many solutions on the internet but it couldn't solve it.

Please help me on this. I am using css3, bootstrap and html. I tried to add passive attribute etc but seems like nothing is working.

My code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Google Tag Manager -->
    <script>
        (function (w, d, s, l, i) {
            w[l] = w[l] || []; w[l].push({
                'gtm.start':
                    new Date().getTime(), event: 'gtm.js'
            }); var f = d.getElementsByTagName(s)[0],
                j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
                    'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
        })(window, document, 'script', 'dataLayer', 'GTM-W9FNBLV');</script>
    <!-- End Google Tag Manager -->
    <!--  -->
    <!--    Document Title-->
    <!-- =============================================-->
    <!--<title>Velosi | Asset Integrity Ltd.</title>-->
    <title>@ViewBag.Title</title>
    @RenderSection("head", false)
    <!--  -->
    <!--    JavaScripts-->
    <!--    =============================================-->
    <script src="~/Scripts/modernizr-2.8.3.js"></script>
    @*<script src="~/Scripts/modernizr-2.8.3.js"></script>*@
    <link href="~/Content/Theme/lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script src="~/Scripts/popper.min.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,500,600,700|Open+Sans:300,400,600,700,800" rel="stylesheet" />
    <link href="~/Content/Theme/lib/iconsmind/iconsmind.css" rel="stylesheet">
    <link href="~/Content/External/css/ionicons.min.css" rel="stylesheet" />
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script src="~/Content/Theme/lib/imagesloaded/imagesloaded.pkgd.min.js"></script>
    <script src="~/Content/Theme/lib/gsap/src/minified/TweenMax.min.js"></script>
    <script src="~/Content/Theme/lib/gsap/src/minified/plugins/ScrollToPlugin.min.js"></script>
    <script src="~/Content/Theme/lib/CustomEase.min.js"></script>
    <script src="~/Content/Theme/js/config.js"></script>
    <script src="~/Content/Theme/js/zanimation.js"></script>
    <script src="~/Content/Theme/lib/owl.carousel/dist/owl.carousel.min.js"></script>
    <script src="~/Content/Theme/lib/remodal/dist/remodal.js"></script>
    <script src="~/Content/Theme/lib/lightbox2/dist/js/lightbox.js"></script>
    <script src="~/Content/Theme/lib/flexslider/jquery.flexslider-min.js"></script>
    <script src="~/Content/Theme/js/core.js"></script>
    <script src="~/Content/Theme/js/main.js"></script>
    <script src="~/Content/External/js/jquery.slidereveal.min.js"></script>
    <!-- DataTable -->
    <script src="~/Content/Theme/DataTables-1.10.18/js/jquery.dataTables.min.js"></script>
    <script src="~/Content/Theme/DataTables-1.10.18/js/dataTables.bootstrap.min.js"></script>
    <script src="~/Content/Theme/FixedHeader-3.1.4/js/dataTables.fixedHeader.min.js"></script>
    <script src="~/Content/Theme/Responsive-2.2.2/js/dataTables.responsive.min.js"></script>
    <script src="~/Content/Theme/Responsive-2.2.2/js/responsive.bootstrap.min.js"></script>
    <!-- Datepicker -->
    <script src="~/Content/Theme/select2/dist/js/select2.min.js"></script>
    <script src="~/Content/Theme/Toaster/toastr.js"></script>
    @RenderSection("scripts", required: false)


    <!--    Favicons-->
    <!--    =============================================-->
    <link rel="apple-touch-icon" sizes="180x180" href="~/Content/Theme/images/favicons/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="~/Content/Theme/images/favicons/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="~/Content/Theme/images/favicons/favicon-16x16.png">
    <link rel="shortcut icon" type="image/x-icon" href="~/Content/Theme/images/favicons/favicon.ico">
    @*<link rel="manifest" href="~/Content/Theme/images/favicons/manifest.json">*@
    <link rel="mask-icon" href="~/Content/Theme/images/favicons/safari-pinned-tab.svg" color="#5bbad5">
    <meta name="msapplication-TileImage" content="~/Content/Theme/images/favicons/mstile-150x150.png">
    <meta name="theme-color" content="#ffffff">
    <!--  -->
    <!--    Stylesheets-->
    <!--    =============================================-->
    <!-- Default stylesheets-->
    <!-- Template specific stylesheets-->
    <link href="~/Content/Theme/lib/loaders.css/loaders.min.css" rel="stylesheet" />
    <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,500,600,700|Open+Sans:300,400,600,700,800" rel="stylesheet">
    <link href="~/Content/Theme/lib/iconsmind/iconsmind.css" rel="stylesheet">
    <link href="~/Content/External/css/ionicons.min.css" rel="stylesheet" />
    <link href="~/Content/Theme/lib/hamburgers/dist/hamburgers.min.css" rel="stylesheet">
    <link href="~/Content/Theme/lib/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="~/Content/Theme/lib/owl.carousel/dist/assets/owl.carousel.min.css" rel="stylesheet">
    <link href="~/Content/Theme/lib/owl.carousel/dist/assets/owl.theme.default.min.css" rel="stylesheet">
    <link href="~/Content/Theme/lib/remodal/dist/remodal.css" rel="stylesheet">
    <link href="~/Content/Theme/lib/remodal/dist/remodal-default-theme.css" rel="stylesheet">
    <link href="~/Content/Theme/lib/flexslider/flexslider.css" rel="stylesheet">
    <link href="~/Content/Theme/lib/lightbox2/dist/css/lightbox.css" rel="stylesheet">
    <link href="~/Content/Theme/Toaster/toastr.css" rel="stylesheet" />
    <!-- Main stylesheet and color file-->
    <link href="~/Content/Theme/css/style.css" rel="stylesheet">
    <link href="~/Content/Theme/css/custom.css" rel="stylesheet">
    <!-- DataTable -->
    <link href="~/Content/Theme/DataTables-1.10.18/css/dataTables.bootstrap.min.css" rel="stylesheet" />
    <link href="~/Content/Theme/FixedHeader-3.1.4/css/fixedHeader.bootstrap.min.css" rel="stylesheet" />
    <link href="~/Content/Theme/Responsive-2.2.2/css/responsive.bootstrap.min.css" rel="stylesheet" />
    <!-- Datepicker -->
    <link href="~/Content/Theme/select2/dist/css/select2.min.css" rel="stylesheet" />
    <link href="~/Content/Site.css" rel="stylesheet" />




</head>
<body data-spy="scroll" data-target=".inner-link" data-offset="60">
    <!-- Google Tag Manager (noscript) -->
    <noscript>
        <iframe src="https://www.googletagmanager.com/ns.html?id=GTM-W9FNBLV"
                height="0" width="0" style="display:none;visibility:hidden"></iframe>
    </noscript>
    <!-- End Google Tag Manager (noscript) -->

    <main>
        <div class="loading" id="preloader">
            <div class="loader h-100 d-flex align-items-center justify-content-center">
                <div class="line-scale">
                    <div></div>
                    <div></div>
                    <div></div>
                    <div></div>
                    <div></div>
                </div>
            </div>
        </div>
        @Html.Partial("_Header")


        @*<button class="btn btn-md btn-round btnEmailUs RequestCallBackIcon" id="trigger"><i class="fa fa-phone fa-lg"></i></button>*@
        <div style="display: flex; justify-content: flex-end;" class="m-2">
            <button class="right btn btn-md btn-round btnEmailUs1 RequestCallBackIcon" id="trigger"><i class="fa fa-phone fa-lg"></i></button>
        </div>


        <div id='slider' class="card text-white bg-dark EnquiryLowerBody" style="max-width: 20rem; display:none;">
            <div class="card-header header-callback">Request Callback</div>
            @using (Html.BeginForm("EnquiryForm", "Home", null, FormMethod.Post, new { @class = "mt-3" }))
            {
                <div class="card-body CardBody">
                    <div class="form-group">
                        <div class="col-md-12">
                            <input type="text" id="name" name="name" oninput="this.value = this.value.replace(/[0-9]/, '');" class="form-control" placeholder="Your name" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-12">
                            <input type="text" oninput="this.value = this.value.replace(/[^0-9]/g, '');" id=" mobileno" name="mobileno" class="form-control" placeholder="00971 XX XXXXXXX" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-12">
                            <input type="email" id="email" name="email" class="form-control" placeholder="name@domain.com" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-12">
                            <select class="d-flex flex-wrap" name="MainServiceName" id="MainServices">
                                <option selected value="m-None">-Select-</option>
                                <option value="m-SS">Software Services</option>
                                <option value="m-AIMS">Asset Integrity Management Services</option>
                                <option value="m-HSE">HSE & Environmental Services</option>
                                <option value="m-ES">Engineering Services</option>
                                <option value="Other">Other</option>
                            </select>
                        </div>
                    </div>

                    <div id="divSubServices" class="form-group">
                        <div class="col-md-12">
                            <select class="d-flex flex-wrap" name="SubServiceName" id="SubServices">

                                <option class="" selected value="s-None">-Select-</option>
                                <option class="SoftwareServices" value="s-AIMS">VAIL-Plant (Asset Integrity Management System)</option>
                                <option class="SoftwareServices" value="s-PHA">VAIL-PHA (Process Hazard Analysis)</option>
                                <option class="SoftwareServices" value="s-PSRA">VAIL-PSRA (Petrol Station Risk Assessment)</option>
                                <option class="SoftwareServices" value="s-MTS">VAIL-MTS (Material Tracking System)</option>

                                <option class="AIMS" value="s-RBI">Risk Based Inspection (RBI)</option>
                                <option class="AIMS" value="s-RCM">Reliability Centered Maintenance(RCM)</option>
                                <option class="AIMS" value="s-SIL">Safety Integrity Level (SIL)</option>
                                <option class="AIMS" value="s-PIMS">Pipeline Inspection Management System (Onshore & Offshore)</option>

                                <option class="HSE" value="s-HSECES">Health, Safety & Environmental Critical Equipment Systems (HSECES)</option>
                                <option class="HSE" value="s-AM">Risk Assessment & Management</option>
                                <option class="HSE" value="s-OH">Occupational Health</option>
                                <option class="HSE" value="s-EMP">Emergency Management & Planning</option>
                                <option class="HSE" value="s-HS">HSEIA Studies</option>
                                <option class="HSE" value="s-ES">Environmental Services</option>

                                <option class="EngineeringServices" value="s-FFS">Fitness for Services(FFS)</option>
                                <option class="EngineeringServices" value="s-CDFDECS">Conceptual Design, FEED, Detailed Design and Engineering Consultancy Services</option>
                                <option class="EngineeringServices" value="s-OMP">Operating Manuals & Procedures</option>
                                <option class="EngineeringServices" value="s-DVA">Design Verification & Appraisal</option>
                                <option class="EngineeringServices" value="s-ABDS">As-Built Drafting Services</option>

                                <option value="Other">Other</option>
                            </select>
                        </div>
                    </div>

                    <div id="divDescription" class="form-group">
                        <div class="col-md-12">
                            <input type="text" id="Description" name="Description" class="form-control" placeholder="Description">
                        </div>
                    </div>
                    <div id="divDescription" class="form-group">
                        <div class="col-md-12">
                            <button id="btnSubmit" class="btn btn-primary BtnSubmitEnquiry">Submit</button>
                        </div>
                    </div>

                </div>
            }

        </div>

        @RenderBody()
        <div id="cookieConsent">
            <div id="closeCookieConsent">x</div>
            We use tools, such as cookies, to enable essential services and functionality on our site and to collect data on how visitors                   interact with our site, products and services. <a class="cookieConsentOK">Got it</a>
        </div>

        @Html.Partial("_Footer")

    </main>
    <!--  -->

</body>
</html>

<script type="text/javascript">
    $(document).ready(function () {

        $("#divDescription").hide();
        $("#divSubServices").hide();

        if (!sessionStorage.getItem("firstVisit")) {

            sessionStorage.setItem("firstVisit", true);

            setTimeout(function () {
                $("#cookieConsent").fadeIn(200);
            }, 4000);
            $("#closeCookieConsent, .cookieConsentOK").click(function () {
                $("#cookieConsent").fadeOut(200);
            });
        }

        //setTimeout(function () {
        //    $("#cookieConsent").fadeIn(200);
        //}, 4000);
        //$("#closeCookieConsent, .cookieConsentOK").click(function () {
        //    $("#cookieConsent").fadeOut(200);
        //});


    });

    $("#trigger").click(
        function()
        {
            $("#slider").show();


        }
    );

    $("#slider").slideReveal({
        trigger: $("#trigger"),
        position: "right",
        push: false,
        overlay: true

    });



    $("#MainServices").change(function () {
        var OptionSelected = $(this).find("option:selected");
        var ValueSelected = OptionSelected.val();

        if (ValueSelected == "Other") {

            $("#SubServices").val("s-None");
            $('#divSubServices').hide();
            $('#divDescription').show();

        }
        else {
            $('#divSubServices').show();
            $('#divDescription').hide();
        }

        //Software Services

        if (ValueSelected == "m-None") {

            $("#SubServices").val("s-None");
            $('#divSubServices').hide();

            return;
        }

        if (ValueSelected == "s-None") {

            $('#divDescription').hide();

            return;
        }

        if (ValueSelected == "m-SS") {

            $("#SubServices").show();
            var drpSubServices = $('#SubServices');

            drpSubServices.children('option[class=SoftwareServices]').show();

            drpSubServices.children('option[class=HSE]').hide();

            drpSubServices.children('option[class=AIMS]').hide();

            drpSubServices.children('option[class=EngineeringServices]').hide();

            return;

        }

        //AIMS
        if (ValueSelected == "m-AIMS") {

            $("#SubServices").show();

            var drpSubServices = $('#SubServices');

            drpSubServices.children('option[class=SoftwareServices]').hide();

            drpSubServices.children('option[class=HSE]').hide();

            drpSubServices.children('option[class=AIMS]').show();

            drpSubServices.children('option[class=EngineeringServices]').hide();

            return;

        }

        //HSE
        if (ValueSelected == "m-HSE") {

            $("#SubServices").show();

            var drpSubServices = $('#SubServices');

            drpSubServices.children('option[class=SoftwareServices]').hide();

            drpSubServices.children('option[class=HSE]').show();

            drpSubServices.children('option[class=AIMS]').hide();

            drpSubServices.children('option[class=EngineeringServices]').hide();

            return;

        }

        //Engineering Services
        if (ValueSelected == "m-ES") {

            $("#SubServices").show();

            var drpSubServices = $('#SubServices');

            drpSubServices.children('option[class=SoftwareServices]').hide();

            drpSubServices.children('option[class=HSE]').hide();

            drpSubServices.children('option[class=AIMS]').hide();

            drpSubServices.children('option[class=EngineeringServices]').show();

            return;

        }
    });

    $("#SubServices").change(function () {
        var OptionSelected = $(this).find("option:selected");
        var ValueSelected = OptionSelected.val();
        if (ValueSelected == "Other") {

            $('#divDescription').show();

        }
        else {

            $('#divDescription').hide();
        }
    });

    $("#btnSubmit").click(function () {

        var name = $("#name").val();
        var email = $("#email").val();
        var mobileno = $("#mobileno").val();
        var MainServiceName = $("#MainServices").find(":selected").text();
        var SubServiceName = $("#SubServices").find(":selected").text();
        var Description = $("#Description").val();


        var EnquiryForm = {
            'name': name,
            'email': email,
            'mobileno': mobileno,
            'MainServiceName': MainServiceName,
            'SubServiceName': SubServiceName,
            'Description': Description
        }
        //alert(EnquiryFormViewModel.name);
        @*$.post("/Home/EnquiryForm", EnquiryForm, function(returnedData) {
            //alert(returnedData.data);

            window.location.href= '@Url.Action("Thankyou", "Others", new {message= "Thank you for contacting us. We'll get back to you soon." })';

        });*@



    });

</script>
Johnny Shallow
  • 101
  • 1
  • 3
  • 12
  • 1
    Possible duplicate of [What are passive event listeners?](https://stackoverflow.com/questions/37721782/what-are-passive-event-listeners) – CertainPerformance Apr 06 '19 at 10:42
  • 1
    Using a passive listener means you're promising not to call `preventDefault`. If you want to be able to do that, don't use a passive listener. – CertainPerformance Apr 06 '19 at 10:42
  • @CertainPerformance: how do i stop it? – Johnny Shallow Apr 06 '19 at 10:52
  • I have seen the post but not sure where to use that code – Johnny Shallow Apr 06 '19 at 10:52
  • Chrome has set the passive event listener as the default (ignoring the standard) so there's no way around it anymore, CertainPerformance. – Shikkediel Apr 06 '19 at 10:52
  • but there must be some way – Johnny Shallow Apr 06 '19 at 10:53
  • @Shikkediel No, you just have to explicitly note that you want to add a non-passive listener - see the link in OP's question – CertainPerformance Apr 06 '19 at 10:54
  • No that's wrong, Chrome 73 has set it to `true` for all scripts. That said, I do not see any instance of `preventDefault()` or `return false` in this script. I suspect something else is causing it. – Shikkediel Apr 06 '19 at 10:56
  • @CertainPerformance: where do I add that code? – Johnny Shallow Apr 06 '19 at 10:59
  • First identify where exactly in your code you're calling `preventDefault` – CertainPerformance Apr 06 '19 at 11:00
  • Explicitly no where but that error increases in number when i scroll – Johnny Shallow Apr 06 '19 at 11:00
  • Look for `wheel` or `touch` events, they will cause this when used on the document level. – Shikkediel Apr 06 '19 at 11:01
  • @Shikkediel passive: false works fine for me in Chrome 73: https://jsfiddle.net/ntyv6au1/ – CertainPerformance Apr 06 '19 at 11:02
  • @Shikkediel: this var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'], toBind = ( 'onwheel' in document || document.documentMode >= 9 ) ? ['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'], slice = Array.prototype.slice, nullLowestDeltaTimeout, lowestDelta; ? – Johnny Shallow Apr 06 '19 at 11:03
  • @CertainPerformance: that means that I would have to disable mouse scrolling function from my code? User would not be able to scroll with wheel? That's gross – Johnny Shallow Apr 06 '19 at 11:05
  • Why wouldn't that work, CertainPerformance? The point is that it is set to `true` by default which is against the standard. Try [removing](https://codepen.io/anon/pen/RObdoM?editors=0010) it, you'll see it's not working as expected. It can be fixed, Johhny Shallow. – Shikkediel Apr 06 '19 at 11:06
  • @Shikkediel You said "there's no way around it anymore" but you can just add `passive: false` to get around it, right? – CertainPerformance Apr 06 '19 at 11:08
  • @JohnnyShallow No, that was just an example of how you *can* attach a non-passive listener. – CertainPerformance Apr 06 '19 at 11:08
  • This is getting ridiculous. There is no way around adapting to passive event listerners anymore because Chrome is forcing them upon us in this way. I think it was very clear what I meant. Done here. – Shikkediel Apr 06 '19 at 11:10
  • @Shikkediel: so it would remain like it is? no solution? – Johnny Shallow Apr 06 '19 at 11:11
  • I mean no proper solution – Johnny Shallow Apr 06 '19 at 11:11
  • There is a proper solution - identifying the event in question and setting `passive: false` inside the event listener. It is recommended to do this through feature detection though because IE for example will break the script otherwise (they can't handle the option). [Here](https://css-tricks.com/forums/topic/beware-of-passive-event-listeners-2/) are some hints I posted recently. Also, you won't be able to do this with jQuery yet, it will need a vanilla event listener until 4.0 has fixed that. – Shikkediel Apr 06 '19 at 11:28
  • See https://stackoverflow.com/questions/55955171/preventing-unable-to-preventdefault-inside-passive-event-listener-error-within for a polyfill script which reverts Chrome to default to `passive: false` and is cvompatible with other browsers (based on an earlier polyfill which defaulted to `passive: true` similar to the latest Chrome). – Mike May 04 '19 at 14:05
  • Have you folks seen this? https://github.com/zzarcon/default-passive-events Easy fix: `` – zipzit Jul 24 '21 at 14:38

2 Answers2

7

I know it's a bit old. But I found a solution for my likely problem.

The problem is; wheel event needs to be passive or not. Modern browsers such as chrome threated as it passive=true. So exception rises.

Find addition of wheel event listener something like "window.addEventListener('wheel', func)" or "window.addEventListener('wheel', func, true|false)" and change it as "window.addEventListener('wheel', func, {passive:false})".

My problem solved like that.

ferhatozkanun
  • 129
  • 1
  • 5
  • This answer is great! It would be perfect if it had some documentation reference like this: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener – Sanxofon May 14 '21 at 17:00
0

If your code is minified like it was with a plugin that I've installed in wordpress called advanced-scrollbar/js/jquery.nicescroll.min.js, then you should like FerhatOzkanun stated above look for specifics in the source code and update it accordingly, clear the cache and re-run then inspect once again. For me this solution worked..

You may look for something like this firstly:

/*comment this ->return*/b.preventDefault(); /*add this*/{passive:false}

and look for this instance and replace it.

/*comment this ->e||!1*/ /*add this*/{passive:false}

which refers to window.addEventListener('wheel', func, true|false) mentioned before.

Jean G.T
  • 1
  • 10
  • 25