简体   繁体   English

jQuery document.ready没有警报就不会触发

[英]Jquery document.ready not triggered without alert

I'm facing this very weird issue that my function in document ready is not triggered, unless I put alert after the function. 我面临着一个非常奇怪的问题,即除非我在函数后加了alert ,否则不会触发文档就绪中的函数。 I found this out when I debug using the alert , and apparently everything was working fine. 当我使用alert调试时,我发现了这一点,显然一切正常。 But when I removed the alert , function 'RaiseEvent' never get called. 但是,当我删除alert ,永远不会调用函数'RaiseEvent'。

Here's my HTML: 这是我的HTML:

<script src="../Content/jquery.mobile-1.4.2/js/jquery.js"></script>
<script src="../Content/jquery.mobile-1.4.2/js/jquery.mobile-1.4.2.min.js"></script>
<script type="text/javascript" src="Scripts/hybrid.js"></script>
<script>
$(document).ready(function(){
            //populate form
            //alert('Calling POPULATE-FORM');
            RaiseEvent('POPULATE-FORM');
            //alert('After POPULATE-FORM');
});
</script>

The RaiseEvent function is retrieved from hybrid.js: RaiseEvent function是从hybrid.js检索的:

function RaiseEvent(eventName)
{
    if (!eventName) eventName = '';
    var qs = '';
    var elms = document.getElementsByTagName('*');
    for (var i = 0; i < elms.length; i++) {
        if (elms[i].name) {
            qs += (qs.length > 0 ? '&' : '') + encodeURIComponent(elms[i].name) + '=' + encodeURIComponent(elms[i].value);
        }
        if (elms[i].type == 'checkbox' && elms[i].checked)
            qs += (qs.length > 0 ? '&' : '') +
            'checked:' + encodeURIComponent(elms[i].name) + '=1';
    }
    location.href = 'xpostback:' + eventName + ':' + qs;
}

I've googled this issue and found few people facing this also Here but I followed his solution already to no avail. 我GOOGLE了这个问题,并发现面临这样也很少有人在这里 ,但我跟他的解决方案已经无济于事。

Anyone facing the same issue or have any suggestions/advice what might go wrong? 任何人遇到相同的问题或有任何建议/建议可能会出什么问题?

I have some thoughts on your problem. 我对你的问题有一些想法。

a) Callback function in ready() a)ready()中的回调函数

From documentation handler is callback function which means that when DOM element is ready your function is beeing called. 来自文档handler是回调函数,这意味着当DOM元素准备好时,您的函数就会被调用。 I suppose that is not the problem. 我想那不是问题。

document.ready( handler );

b) Jquery.mobile b)Jquery.mobile

Fast googling told me that you could use different function. 快速谷歌搜索告诉我,您可以使用其他功能。 See pagecreated documentation. 请参阅页面创建的文档。

 $(document).on('pagecreated',function(){ 
                RaiseEvent('POPULATE-FORM');
            });

Also look here: 也请看这里:

  1. jQuery mobile $(document).ready equivalent jQuery mobile $(document).ready等效项
  2. jQuery Mobile: document ready vs page events jQuery Mobile:准备文档与页面事件

c) Error in function RaiseEvent(eventName) c)函数RaiseEvent(eventName)中的错误

Even if your function works with alert this doesn't guarantee that you function is working properly. 即使您的功能具有alert功能,也不能保证您的功能正常工作。 I had a lot of situations that in all modern browsers my code works but there was some bugs. 我遇到过很多情况,在所有现代浏览器中,我的代码都可以运行,但存在一些错误。 Only Internet Explorer was so kind and throw me errors. 只有Internet Explorer非常友好,并抛出错误。 I suggest running your code with JS debugger. 我建议使用JS调试器运行代码。

Summary 摘要

I would start from b) and then try to look at c) . 我将从b)开始,然后尝试查看c) Good luck :) 祝好运 :)

Apparently there is another "document.ready" function in hybrid.js that caused inconsistent RaiseEvent calling. 显然,hybrid.js中还有另一个“ document.ready”函数,导致RaiseEvent调用不一致。 Probably because the asynchronous nature of Javascript, the RaiseEvent('POPULATE-FORM') get overlapped by the RaiseEvent('DOCUMENT-READY') in hybrid.js: 可能是由于Javascript的异步特性,导致hybrid.js中的RaiseEvent('POPULATE-FORM')与RaiseEvent('DOCUMENT-READY')重叠:

var readyStateCheckInterval = setInterval(function() {
    if (document.readyState === "complete") {
        RaiseEvent("DOCUMENT_READY");
        Init();
        clearInterval(readyStateCheckInterval);
    }
}, 50);

Credits to @Barmar for helping me debugging the isssue! 感谢@Barmar帮助我调试了问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM