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 5974017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:52:58+00:00 2026-05-22T20:52:58+00:00

I am trying to parse the HTML of a webpage to DOM by loading

  • 0

I am trying to parse the HTML of a webpage to DOM by loading it into an iframe and do some searching on the DOM afterwards. Here’s the code

function f(callback) {
    var tmp = document.createElement('iframe');
    $(tmp).hide();
    $(tmp).insertAfter($('foo'));
    $(tmp).attr('src', url);

    $(tmp).load(function() {
        var bdy = tmp.contentDocument.body;
        callback(bdy);
        $(tmp).remove();
    });
}

In the callback function if I do something like the following

function callback(bdy) {
    alert($(bdy).find('bar').length);
}

sometimes it gives me the correct value but sometimes it gives me 0 instead. However, if I do the following it works

var tmp = document.createElement('iframe');
$(tmp).hide();
$(tmp).insertAfter($('foo'));
$(tmp).attr('src', url);

$(tmp).load(function(tmp) {
    setTimeout(function() {
        var bdy = tmp.contentDocument.body;
        callback(bdy);
        $(tmp).remove();
    }, '100');
});

Since setTimeout() depends on the client’s end, I would like to know if there is any better way to achieve the same goal. 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-22T20:52:59+00:00Added an answer on May 22, 2026 at 8:52 pm

    Check this question out, along with its many answers and related questions.


    Update; here’s some code that waits for #bar to get loaded:

    function f(callback) {
        var tmp = document.createElement('iframe'),
            $tmp = $(tmp);
        $tmp.hide()
            .insertAfter($('foo'))
            .attr('src', url);
    
        $tmp.load(function() {
            var bdy = tmp.contentDocument.body,
                $bdy = $(bdy); // small optimization
    
            var waitForBar = function() {
                if($bdy.find('#bar').length > 0) {
                    callback(bdy);
                    $tmp.remove();
                } else
                    setTimeout(waitForBar, 50);
            };
            waitForBar();
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse a html doc using some code I found from this
Trying to parse an HTML document and extract some elements (any links to text
I'm trying to parse some returned html (from http://www.google.com/movies?near=37130 )to look for currently playing
I have some HTML I am trying to parse. There are cases where the
I'm trying to parse the HTML of a webpage that requires being logged in.
Hi so i am trying to parse a webpage with HTML Agilty Pack to
I am trying to get HTML code from a webpage that is not in
I've been searching for a command line tool that would turn html code into
I am trying to get a webpage to parse using the following code. <var-def
I'm trying to parse a html page and extract 2 values from a table

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.