$ (document). already called inside an external function?

I saw somewhere some jquery code on the net that took this form:

<script>            
function doSomething(message)
{   
    $(document).ready(function(){       
        alert(message);
        });
};
</script>

i.e. an external function ("doSomething") that has $ (document) .ready inside it. I got confused because this is not the code under $ (document) .ready called when loading the DOM? This is similar to an event handler inside a function (?). Does this form of code make sense to everyone? Thank.

+5
source share
4 answers

, . , doSomething . : DOM , ready, jQuery , , , - doSomething. , , — , , , : -) — .

+4

. $(document).ready , , DOM . , , - . DOM, .

Javascript DOM, , , , DOM. , , DOM.

+8

?

, .

+3

$().ready(); , , DOM . . , .ready(), doSomething();

:

<script type="text/javascript">
    function doSomething(){
        $(document).ready(function(){
            alert('here');

        });
    }
    doSomething();
</script>

Put this in different parts of your HTML and check it (:

Hooray!

+1
source

All Articles