blockUI exception 'parentNode' of undefined
Asked Answered
C

3

8

I have huge javascript code and blocking unblocking UI through your blockUI.js (http://malsup.com/jquery/block/)

I get "Cannot read property 'parentNode" of undefined" exception randomly.

It seems like blocking/unblocking sequence in my code got out of sync.ex. because of multiple html template it might be blocking twice and unblocking once or vice versa. It is hard for me to analyze all the code and fix the order.

I was able to generate a fiddle of an issue. Can somebody please take a look and advise a quick fix?

http://jsfiddle.net/pareshvarde/D8KW4/

<script type="text/javascript">
    $(function () {
        $("#blockButton").click(function () {
            myBlock($('#blockSection'));
        });

        window.setInterval(function () {
            myBlock();
            myBlock($('#blockSection'));
            window.setTimeout(function () {
                myUnblock();
                myUnblock($('#blockSection'));
            }, 5000)
        }, 2000);

        $("#unBlockButton").click(function () {
            myUnblock($('#blockSection'));
        });
    });

    myBlock = function (surroundingControl, message) {
        console.log('blocking');

        if (message)
            $("#loader h4").text(message);
        else
            $("#loader h4").text('Loading...');

        if (surroundingControl)
            surroundingControl.block({ message: $('#loader'), baseZ: 1200 });
        else {
            $.blockUI.defaults.message = $('#loader');
            $.blockUI.defaults.baseZ = 1200;
            $.blockUI.apply();
        }
    };

    myUnblock = function (surroundingControl) {
        console.log('unblocking');

        if (surroundingControl)
            surroundingControl.unblock();
        else
            $.unblockUI.apply();
    };
</script>
Chelonian answered 14/10, 2013 at 14:27 Comment(0)
C
3

OK finally I fixed the issue. Basically I created a dynamic element and placed content of my loader into that div and used it for blocking.

my upldated myBlock function as follows:

myBlock = function (surroundingControl, message) {            
        console.log('blocking');

        if (message)
            $("#loader h4").text(message);
        else
            $("#loader h4").text('Loading...');

        var messageContent = document.createElement('div');
        if ($('#loader') !== undefined)
            $(messageContent).html($('#loader').html());
        else
            $(messageContent).html("Loading....");

        if (surroundingControl)
            surroundingControl.block({ message: messageContent, baseZ: 1200 });
        else {
            $.blockUI.defaults.message = messageContent;
            $.blockUI.defaults.baseZ = 1200;
            $.blockUI.apply();
        }
    };
Chelonian answered 14/10, 2013 at 17:14 Comment(1)
Adding this div will fix the error automatically. You can customize this div> <div class="growlUI" style="cursor: default; display: none;"> <h1>Growl Notification</h1> <h2>Have a nice day!</h2> </div> malsup.com/jquery/block/#demosAltaf
B
4

Usually this problem comes as it could not find the DOM element that's specified in "message" property or message property was not specified at all, in your case you need to make sure that $('#loader') is returning an element.

Hint: You could even pass message: null in case you just need to block without displaying any content or loading image.

Bohon answered 24/4, 2016 at 21:44 Comment(0)
C
3

OK finally I fixed the issue. Basically I created a dynamic element and placed content of my loader into that div and used it for blocking.

my upldated myBlock function as follows:

myBlock = function (surroundingControl, message) {            
        console.log('blocking');

        if (message)
            $("#loader h4").text(message);
        else
            $("#loader h4").text('Loading...');

        var messageContent = document.createElement('div');
        if ($('#loader') !== undefined)
            $(messageContent).html($('#loader').html());
        else
            $(messageContent).html("Loading....");

        if (surroundingControl)
            surroundingControl.block({ message: messageContent, baseZ: 1200 });
        else {
            $.blockUI.defaults.message = messageContent;
            $.blockUI.defaults.baseZ = 1200;
            $.blockUI.apply();
        }
    };
Chelonian answered 14/10, 2013 at 17:14 Comment(1)
Adding this div will fix the error automatically. You can customize this div> <div class="growlUI" style="cursor: default; display: none;"> <h1>Growl Notification</h1> <h2>Have a nice day!</h2> </div> malsup.com/jquery/block/#demosAltaf
P
1

For me, it was done putting some text, instead of loading the one from an html element.

Before:

 $.blockUI({
        css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '15px',
            '-moz-border-radius': '15px',
            opacity: 1,
            color: '#fff'
        }, message: $('#loadingMessage')
    });

After

 $.blockUI({
        css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '15px',
            '-moz-border-radius': '15px',
            opacity: 1,
            color: '#fff'
        }, message: 'Loading'
    })

This way, it worked, with same result!

Polydactyl answered 7/5, 2019 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.