简体   繁体   English

DOMContentLoaded事件未触发

[英]DOMContentLoaded event not firing

I'm trying to detect when the DOM is ready using DOMContentLoaded. 我正在尝试使用DOMContentLoaded检测DOM准备就绪的时间。

I'm injecting the following JavaScript code into a page: 我将以下JavaScript代码注入页面:

var script = document.createElement('script');  
script.type = 'text/javascript';  
script.text = function addListener() {
    if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", DOMReady, false);
    }
};

function DOMReady() {
    document.location.href = "mydomain://anything.stuff";
}

function inject() {
    document.getElementsByTagName('head')[0].appendChild(script);
    addListener();
}

It gets added by: 它的添加者是:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    static BOOL injected = NO;
    if (!injected)
    {
        NSBundle *bundle = [NSBundle mainBundle];
        NSURL *jsURL = [bundle URLForResource:@"DetectDOMLoaded" withExtension:@"js"];  
        NSData *jsData = [NSData dataWithContentsOfURL:jsURL];
        NSString *jsScriptAsString = [[NSMutableString alloc] initWithData:jsData encoding:NSUTF8StringEncoding];
        [self.webView stringByEvaluatingJavaScriptFromString:jsScriptAsString];
        [self.webView stringByEvaluatingJavaScriptFromString:@"inject();"];
        injected = YES;
    }

If I add alert calls I can see that the call to addEventListener is being successfully made. 如果添加警报调用,可以看到对addEventListener的调用已成功完成。 However the function DOMReady() is never called. 但是,永远不会调用DOMReady()函数。 Any ideas why not? 任何想法为什么不呢?

Thanks 谢谢

The reason is that you inject the javascript in the webViewDidFinishLoad: method. 原因是您将JavaScript注入了webViewDidFinishLoad:方法中。 That method is called after the UIWebLoad has loaded the HTML page. 在UIWebLoad加载HTML页面之后,将调用该方法。 Meaning that the DOMContentLoaded event may already have been fired, before your javascript has been injected. 意味着在注入JavaScript之前,可能已经触发了DOMContentLoaded事件。

I see two possiblities how to fix this problem: 我看到两种解决方法:

  1. If you have access to the HTML that you are loading into the UIWebView, insert the javascript directly into the HTML. 如果您有权访问要加载到UIWebView中的HTML,则将javascript直接插入HTML中。 That guarantees that the javascript is already there when DOMContentLoaded is being fired. 这可以确保在触发DOMContentLoaded时,javascript已经存在。

  2. If you don't have access to the HTML, you could call your DOMReady() JS function directly from the webViewDidFinishLoad: method (like you call inject() now). 如果您无权访问HTML,则可以直接从webViewDidFinishLoad:方法调用DOMReady() JS函数(就像现在调用inject() )。 The DOM will be fully loaded at that point. DOM将在此时完全加载。 The only insecurity with this option is, that the DOM will be fully loaded but might not yet be fully rendered. 此选项的唯一不安全之处在于,将完全加载DOM,但可能尚未完全呈现。 So if you need the size of certain DOM elements, this option is not secure (because the elements might not have been rendered at that point). 因此,如果您需要某些DOM元素的大小,则此选项并不安全(因为此时可能尚未呈现这些元素)。 But if you are just doing something that does not rely on the DOM being fully rendered, this option should be fine. 但是,如果您只是在做一些不依赖于完全呈现DOM的操作,则此选项应该没问题。

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

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