Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

javascript - Clearing a jquery document.ready() call

How do I clear out anonymous functions that are set to trigger via a jQuery document.ready() call?

For example:

<script type="text/javascript">
    //some code sets a doc ready callback
    $(document).ready(function ()
    {
        alert('ready');
    });
    
    //my attempt to prevent the callback from happening
    window.onload = null;
    $(document).unbind("ready");
    
</script>

The alert happens regardless of my attempts to circumvent it. Is there any way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'd probably get the most appropriate answer if you described what problem you're really trying to solve.

jQuery doesn't have a publicly documented way to undo or block document.ready() handlers. If you control the code, you can use a global variable and a conditional like this:

var skipReady = false;
$(document).ready(function ()
{
    if (!skipReady) {
        alert('ready');
    }
});

// skip the document.ready code, if it hasn't already fired
skipReady = true;

Or, if you want to hack into jQuery a bit (beyond the documented interfaces), you can do this:

$(document).ready(function() {
    alert("ready");
});

// stop the ready handler
$.isReady = true;

You can see this last one work here: http://jsfiddle.net/jfriend00/ZjH2k/. This works because jQuery uses the property: $.isReady to keep track of whether it has already fired the ready handlers or not. Setting it to true makes it think it has already fired them so it won't every do it again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...