Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7681051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:15:16+00:00 2026-05-31T18:15:16+00:00

I’m trying to detect when the DOM is ready using DOMContentLoaded. I’m injecting the

  • 0

I’m trying to detect when the DOM is ready using DOMContentLoaded.

I’m injecting the following JavaScript code into a page:

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. However the function DOMReady() is never called. Any ideas why not?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:15:17+00:00Added an answer on May 31, 2026 at 6:15 pm

    The reason is that you inject the javascript in the webViewDidFinishLoad: method. That method is called after the UIWebLoad has loaded the HTML page. Meaning that the DOMContentLoaded event may already have been fired, before your javascript has been injected.

    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. That guarantees that the javascript is already there when DOMContentLoaded is being fired.

    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). The DOM will be fully loaded at that point. The only insecurity with this option is, that the DOM will be fully loaded but might not yet be fully rendered. 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). But if you are just doing something that does not rely on the DOM being fully rendered, this option should be fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.