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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:42:07+00:00 2026-05-15T06:42:07+00:00

I’m trying to traverse an AJAX response, which contains a remote web page (an

  • 0

I’m trying to traverse an AJAX response, which contains a remote web page (an HTML output).

My goal is to iterate through the ‘script’, ‘link’, and ‘title’ elements of the remote page – load them if necessary, and embed its contents to the current page.

Its working great in FF/IE, but for some reason – Chrome & Safari behaves differently:
When I run a .each() loop on the response, Chrome/Safari seems to omit everything that is under the section of the page.

Here’s my current code:

$.ajax({
    url: 'remoteFile.php',
    cache: false,
    dataFilter: function(data) { 
        console.log(data); 
        /* The output seems to contain the entire response, including the <head> section - on all browsers, including Chrome/Safari */

        $(data).filter("link, script, title").each(function(i) {
            console.log($(this)); 
            /* IE/FF outputs all of the link/script/title elements, Chrome will output only those that are not in the <head> section */
        });

        console.log($(data)); 
        /* This also outputs the incomplete structure on Chrome/Safari */

        return data;
    },
    success: function(response) {}
});

I’ve been struggling with this problem for quite a while now, i’ve found some other similar cases on google searches, but no real solution.
This happens on both jQuery 1.4.2, and jQuery 1.3.2.

I really don’t want to parse the response with .indexOf() and .substring() – it seems to me that it will be an overkill for the client.

Many thanks in advance!

  • 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-15T06:42:08+00:00Added an answer on May 15, 2026 at 6:42 am

    I think this has to do with how jQuery processes HTML strings and creates DOM nodes from them. Amongst a bunch of other things, jQuery will create a temporary <div> and set its innerHTML to whatever HTML you pass to $(...) thus producing a bunch of DOM nodes which can be extracted from the <div> and handed back to you as a jQuery collection.

    The problem, I believe, occurs because of the <html>, <head> and <body> elements, all of which don’t respond well to being appended to a <div> element. Browsers tend to behave differently, — some appear to ignore these top-level elements and just hand you back their contents — others completely ignore the elements, and won’t even give you their descendants.

    It seems, the way to avoid this cross-browser issue is to simply replace the troublesome elements with some other fake elements before parsing. E.g.

    $(
        // replace <html> with <foohtml> .. etc.
        data.replace(/<(\/?)(head|html|body)(?=\s|>)/g, '<foo$1$2')
    ).filter("link, script, title").each(function(i) {
        console.log($(this));
        // do your stuff
    });
    

    Also, I don’t think filter will be sufficient, since it won’t target descendent elements. This may be a better approach:

    $(
        // replace <html> with <foohtml> .. etc.
        data.replace(/<(\/?)(head|html|body)(?=\s|>)/g, '<foo$1$2')
    ).find('link,script,title').andSelf().filter("link,script,title").each(function(i) {
        console.log($(this));
        // do your stuff
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
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 trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.