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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:02:57+00:00 2026-06-15T14:02:57+00:00

I am requesting full HTML5 documents via Ajax using jQuery. I want to be

  • 0

I am requesting full HTML5 documents via Ajax using jQuery. I want to be able to parse them and transfer elements to my main page DOM, ideally with all major browsers, including mobile. I don’t want to create an iframe as I want the process to be as quick as possible. With Chrome & Firefox I can do the following:

var contents = $(document.createElement('html'));
contents[0].innerHTML = data; // data : HTML document string

This will create a proper document, somewhat surprisingly, just without a doctype. In IE9, however, one may not use the innerHTML to set the contents of the html element. I tried to do the following, without any luck:

  1. Create a DOM, open it, write to it and close it. Issue: on doc.open, IE9 throws an exception called Unspecified error..

    var doc = document.implementation.createHTMLDocument('');
    doc.open();
    doc.write(data);
    doc.close();
    
  2. Create an ActiveX DOM. This time, the result is better but upon transferring / copying elements between documents IE9 crashes. Bad because no IE8 support (adoptNode / importNode support).

    var doc = new ActiveXObject('htmlfile'); 
    doc.open();
    doc.write(data);
    doc.close();
    contents = $(doc.documentElement);
    document.adoptNode(contents);
    

I was thinking about recursively recreating the elements, instead of transferring them between my documents, but that seems like an expensive task, given that I can have a lot nodes to transfer. I like my last ActiveX example as that will most likely work in IE8 and earlier (for parsing, at least).

Any ideas on this? Again, not only I need to be able to parse the head and body, but I also need to be able to append these new elements to my main dom.

Thanks much!

  • 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-06-15T14:02:58+00:00Added an answer on June 15, 2026 at 2:02 pm

    Answering my own question… To solve my issue I used all solutions mentioned in my post, with try/catch blocks if a browser throws an error (oh, how we love thee IE!). The following works in IE8, IE9, Chrome 23, Firefox 17, iOS 4 and 5, Android 3 & 4. I have not tested Android 2.1-2.3 and IE7.

    var contents = $('');
    try {
      contents = $(document.createElement('html'));
      contents[0].innerHTML = data;
    }
    catch(e) {
      try {
        var doc = document.implementation.createHTMLDocument('');
        doc.open();
        doc.write(data);
        doc.close();
        contents = $(doc.documentElement);
      }
      catch(e) {
        var doc = new ActiveXObject('htmlfile'); 
        doc.open();
        doc.write(data);
        doc.close();
        contents = $(doc.documentElement);
      }
    }
    

    At this point we can find elements using jQuery. Transferring them to a different DOM creates a bit of a problem. There are a couple of methods that do this, but they are not widely supported yet (importNode & adoptNode) and/or are buggy. Given that our selector string is called ‘selector’, below I re-created the found elements and append them to ‘.someDiv’.

    var fnd = contents.find(selector);
    if(fnd.length) {
      var newSelection = $('');
      fnd.each(function() {
        var n       = document.createElement(this.tagName);
        var attr    = $(this).prop('attributes');
        n.innerHTML = this.innerHTML;
        $.each(attr,function() { $(n).attr(this.name, this.value); });
        newSelection.push(n);
      });
      $('.someDiv').append(newSelection);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Requesting data from any location on my domain with .load() (or any jQuery ajax
I am requesting a html5 web page it contains images and sound files. Once
I am requesting something with $.ajax and i am getting a 302 or 200
The Internet is full of developers requesting a 64bit Delphi, and users of Delphi
I'm trying to list only entries in the root via the Google Documents API
When requesting header data using HEAD method with CORS object, it returns null. getAllResponseHeaders()
When requesting (ht|x)ml with ajax you can only send requests to the same domain.
When requesting for words using Wordnik API, you can specify minimum and maximum corpus
Full edit: The scenario is that after uploading the file to the server via
Client is requesting a Lightbox gallery which will, on click, provide image, description and

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.