Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripts that require jQuery are executing before jquery is loaded when using CustomScripts #53

Open
ahmad-shahid opened this issue Aug 3, 2023 · 1 comment

Comments

@ahmad-shahid
Copy link
Contributor

A solution has been presented (ported from https://gccode.ssc-spc.gc.ca/iitb-dgiit/sds/GOCWebTemplates/DotNetTemplates/-/issues/454):

var formSubmit = false;

(function () {
    var a = setInterval(function () {
        if (typeof window.wb === 'undefined') {
            return;
        }
        clearInterval(a);

        $(document).on("wb-ready.wb", function () {
            var x = $("#pageForm").validate();
            x.settings.submitHandler = function (form) {
                if (!formSubmit) {
                    formSubmit = true;
                    form.submit();
                }
            };
        });

    }, 100);
})();
@sgdowney
Copy link

sgdowney commented May 10, 2024

The above solution was not 100% successful in resolving the issue. Using the method below is the recommended solution for adding JS files to the page.

WebTemplateModel.HTMLBodyElements.Add(scriptTag)

In addition to the code above, in the JS file I implemented the following code as the JS file was sometimes loading after the wb-ready.wb event has already fired.

function SetOverride() {
    // code to execute after wb-ready.wb event
}
if (wb.isReady) {
    SetOverride();
}
else {
    $(document).on("wb-ready.wb", function () {
        SetOverride();
    });
}

This seems to be reliable based on testing conducted thus far.
wet-boew/wet-boew#9762 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants