jQuery $(document).ready () fires twice

人盡茶涼 提交于 2019-11-26 09:29:52

问题


So i\'ve been sifting around the web trying to find out whats going on here and I have not been able to get a concrete answer.

I have one $(document).ready on my site that seams to run multiple times regardless of the code that is inside it.

I\'ve read up on the bug reports for jQuery about how the .ready event will fire twice if you have an exception that occurs within your statement. However even when I have the following code it still runs twice:

$(document).ready(function() {
    try{    
        console.log(\'ready\');
        }
    catch(e){
        console.log(e);
    }
});

In the console all I see is \"ready\" logged twice. Is it possible that another .ready with an exception in it would cause an issue? My understanding was that all .ready tags were independent of each other, but I cannot seem to find where this is coming into play?

Here is the head block for the site:

<head>
<title>${path.title}</title>
<meta name=\"Description\" content=\"${path.description}\" />
<link href=\"${cssHost}${path.pathCss}\" rel=\"stylesheet\" type=\"text/css\" />
<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js\" type=\"text/javascript\" charset=\"utf-8\"><!----></script>
<script src=\"media/js/fancybox/jquery.fancybox.pack.js\" type=\"text/javascript\" ><!-- --></script>
<script src=\"/media/es/jobsite/js/landing.js\" type=\"text/javascript\" ><!-- --></script>
<script src=\"/media/es/jobsite/js/functions.js\" type=\"text/javascript\"><!-- -->    </script>
<script src=\"/media/es/jobsite/js/jobParsing.js\" type=\"text/javascript\" charset=\"utf-8\"><!----></script>
<script src=\"/media/es/jobsite/js/queryNormilization.js\" type=\"text/javascript\" charset=\"utf-8\"><!----></script>
<script src=\"${jsHost}/js/jquery/jquery.metadata.js\" type=\"text/javascript\" charset=\"utf-8\"><!----></script>
<script src=\"${jsHost}/js/jquery/jquery.form.js\" type=\"text/javascript\" charset=\"utf-8\"><!----></script>
<script src=\"http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.min.js\" type=\"text/javascript\" charset=\"utf-8\"><!----></script>
<script src=\"${jsHost}/js/jquery.i18n.properties-min.js\" type=\"text/javascript\" charset=\"utf-8\"><!----></script>

<script type=\"text/javascript\" charset=\"utf-8\">

function updateBannerLink() {
    var s4 = location.hash.substring(1);
    $(\"#banner\").attr(\'href\',\'http://INTELATRACKING.ORG/?a=12240&amp;c=29258&amp;s4=\'+s4+\'&amp;s5=^\');
}

</script>
</head>

Pay no attention to the JSP variables, but as you can see i\'m only calling the functions.js file once (which is where the .ready function exists)


回答1:


The ready event cannot fire twice. What is more than likely happening is you have code that is moving or manipulating the element that the code is contained within which causes the browser to re-execute the script block.

This can be avoided by including script tags in the <head> or before the closing </body> tag and not using $('body').wrapInner();. using $('body').html($('body').html().replace(...)); has the same effect.




回答2:


It happened to me also, but I realized that the script had been included twice because of a bad merge.




回答3:


This happened to me when using KendoUI... invoking a popup window would cause the document.ready event to fire multiple times. The easy solution is to set a global flag so that it only runs once:

var pageInitialized = false;
$(function()
{
    if(pageInitialized) return;
    pageInitialized = true;
    // Put your init logic here.
});

It's sort of hack-ish, but it works.




回答4:


Make sure you don't include JS file twice. That was my case




回答5:


You might consider to use

window.onload

instead of

$(document).ready



回答6:


try putting this in your functions.js to prevent it from being executed twice :

var checkit = window.check_var;
if(checkit === undefined){ //file never entered. the global var was not set.
    window.check_var = 1;
}
else {
    //your functions.js content 
}

however i suggest that you look more into it to see where are you calling the second time.




回答7:


I had a similar problem when I was trying to refresh a partial. I called a return ActionResult instead of a return PartialViewResult. The ActionResult caused my ready() to run twice.




回答8:


There is a possibility to encounter this problem when you add same controller twice in the html.
For an instance:
[js]

app.controller('AppCtrl', function ($scope) {
 $(document).ready(function () {
        alert("Hello");
        //this will call twice 
    });
});

[html]

//controller mentioned for the first time
<md-content ng-controller="AppCtrl">
    //some thing
</md-content>

//same controller mentioned again 
<md-content ng-controller="AppCtrl">
    //some thing
</md-content>



回答9:


I had a similar issue today. A <button type="submit"> caused the $(document).ready(...) event to fire again in my case. Changing the code to <button type="button"> solved the issue for me.

See document.ready function called again after submit button? here on stackoverflow for more details.




回答10:


In my case $(document).ready was firing twice because of bad CSS, check if any part of your CSS has background-image: url('');




回答11:


If the iframe doesnt show anything and is used for other reasons (like uploading a file without reload) you can do something like this :

<iframe id="upload_target" name="upload_target" style="width:0;height:0;border:0px solid #fff;"></iframe>

Notice that src is not included that prevents the second on ready trigger on the document.




回答12:


I had this problem with window.load function was executed twice: The reason was because I had reference to the same javascript-file in the main page as well as a .net usercontrol. When I removed the reference in the main page, the load-function was only executed once.



来源:https://stackoverflow.com/questions/10727002/jquery-document-ready-fires-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!