Gabriel Theron
Gabriel Theron

Reputation: 1376

jQuery and document.ready() firing twice

I know it is quite a common issue, but even with research I was not able to understand what goes wrong in my call to document.ready() Javascript function.

For some reason, it gets called twice, even when I don't execute anything else than an alert.

As I said in the title, I am using jQuery, and figured something could come from $(function(){}), so I removed any execution in there as well. Nothing changes, document.ready() is still called twice.

What can be the origin of this issue? How to troubleshoot/solve it?

Thanks in advance!

Here's the code I've tried :

$(function(){
    //$( "#tabs" ).tabs();
});

$(document).ready(function() {
    //getTableEntity("organisation", "getentitytable", "#testtable");
    //standardDataTable('#tableOrga');
    alert("document.ready");
});

Edit : I know I'm using the same function twice. Putting everything in one function doesn't solve the problem.

Upvotes: 3

Views: 7894

Answers (4)

liquid
liquid

Reputation: 175

Do you use a template-Engine such smarty or .net or something else? It could be that there is a double script source for jquery. Then there are side effects.

Upvotes: 6

movever
movever

Reputation: 29

i think this demo will help you

<script>
    $(function(){
        $('#test1').appendTo('#test2');
        console.log(1);
    })
</script>
<div id="test1">
<script>
    $(function(){
        console.log(2);     
    });
</script>
</div>

<div id="test2"></div>

the console.log(2) will fire twice.

Upvotes: 0

Talha
Talha

Reputation: 19272

should use one of them... and put your code on one of the following jquery function

$(function(){
    //$( "#tabs" ).tabs();
});

OR

$(document).ready(function() {
    //getTableEntity("organisation", "getentitytable", "#testtable");
    //standardDataTable('#tableOrga');
    alert("document.ready");
});

$(function(){ is equal to $(document.ready)

Read here on jquery

Upvotes: 1

JohannesAndersson
JohannesAndersson

Reputation: 4620

You can check if the dom is ready with this code. Place code in your javascript source file.

// If the DOM is already ready
if ( jQuery.isReady ) {
// Execute the function immediately
fn.call( document, jQuery );
} // ...

Upvotes: 3

Related Questions