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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:08:09+00:00 2026-05-27T19:08:09+00:00

I am trying to load a page’s body just like here: jQuery: Load body

  • 0

I am trying to load a page’s body just like here: jQuery: Load body of page into variable.

However, in this thread no one provided a working solution because $.load() cuts off the <!DOCTYPE>, <html> and <body> tag by default (afaik). I chose the $.get() method and I already got the page’s entire content as a string, but now I am not able to get just the <body> tag (or rather: what’s inside the <body> tag).

So far I have tried:

$.get(uri, function(data){
console.log(data); // --> the entire page's content is logged
});

$.get(uri, function(data){
console.log($(data)); // --> i guess that's the entire site as an object
});

$.get(uri, function(data){
console.log($(data).find("body")); // --> this should be the <body> tag as an object, but console just outputs "[ ]"
});
  • 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-27T19:08:10+00:00Added an answer on May 27, 2026 at 7:08 pm

    Explanation

    Hm.. let’s see if I can properly demonstrate this.

    $.get() is a shorthand for $.ajax().

    So when you do this

    $.get(uri, function(data){
        console.log(data); // --> the entire page's content is logged
    });
    

    You’re really doing this

    $.ajax({
        url: uri,
        type: "GET",
        success: function(msg){
            console.log(msg);
        }
    });
    

    And by default, it returns the page as HTML. Or rather, by default, it first checks the MIME-type on the page, and if none is found, it returns HTML. If you want to tell it what you would like to return, you can either do it in the MIME-type on the server page, or you could use $.getJSON()

    If you want the data returned from your request in form of an object, JSON is the way to go.
    The only real difference in the code, really, is

    • replace your $.get() with $.getJSON()

      $.getJSON(uri, function(data){
          console.log(JSON.stringify(data));
      });
      

    or

    • add dataType: "json" in the $.ajax()

      $.ajax({
          url: uri,
          type: "GET",
          dataType: "json",
          success: function(data){
              console.log(JSON.stringify(data));
          }
      });
      

    so it can expect JSON data to be returned from the page.

    Now all you need to do is prepare the data on the server side, using json_encode()

    $output = array(
        "msg" => "This is output", 
        "data" => array(
            "info" => "Spaaaace", 
            "cake" => "no"
        ), 
        array(
            "foo", 
            "bar"
        )
    );
    echo json_encode($output); 
    //it will look like this before the text is parsed into JSON in Javascript
    //{"msg":"This is output","data":{"info":"Spaaaace","cake":"no"},"0":["foo","bar"]}
    

    This is the way to go if you want objects returned from a request.


    Solution

    Apart from server-side fix with the json_encode(), this is the solution.

    $.getJSON(uri, function(data){
        console.log(JSON.stringify(data)); 
    });
    

    Alternative solution

    Assuming you want to keep your $.get()
    All you need is the text between <body> and </body>
    Here’s an example

    $.get(uri, function(msg){
        var startWith = "<body>",
            endWith = "</body>";
        var iStart = msg.search(startWith);
        var iEnd = msg.search(endWith);
    
        msg= msg.substring(iStart+startWith.length, iEnd)
        console.log(msg);
    });
    

    And here’s a more advanced answer on that one.

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

Sidebar

Related Questions

I'm trying to load one page into another using the .load() method. This loaded
I am trying to load an external jquery page into a div. I use
I'm trying to load an html page from a jsp file. like this. I
i am trying to put a pagination page into jquery. example.html?id=foo&get=23 i would like
I am trying to load mschart in my aspx page(SalesOverview.aspx) using jQuery.load() method, I
I am trying to load generics within the aspx page. Here is my code
I'm trying to load a DIV element from an external page into my current
I'm trying to load the page that describes these 'functions'. However, R console in
I'm trying to load a html page from the assets directory. I tried this,
I am trying to load a div into my page (it is an iteration

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.