1

I'm trying to use both $.ajax or .load() functions to load a page by ajax and than add its content replacing my current $('body').html(). The problem is than when I try to do it jquery will add everything (included the content of <head></head> in my body).

$.ajax({
  type: 'GET',
  url: '/somepage.html',
  success: function(data) {
    $('body').html(data);
  }
});

or

$('body').load('/somepage.html');

The result will be something like

<body>
  <-- Here starts the content of the head -->
  <title>....</title>
  <script src="..."></script>
  <link src="..."></link>
  <meta></meta>
  <-- Here ends the content of the head -->
  <-- Here starts the real body content -->
  <div>........</div>
  <-- Here ends the body content -->
</body>

How can I avoid this? Am I doing something wrong?

Brombomb
  • 6,988
  • 4
  • 38
  • 57
Bagbyte
  • 845
  • 2
  • 18
  • 34

1 Answers1

0

From the jQuery load docs loading a page fragment is done as follows:

$('body').load('/somepage.html body');

By adding a second option to the url argument you can control what get loaded. You can also use css selectors in the second part such as #someId or .someClass.

Brombomb
  • 6,988
  • 4
  • 38
  • 57
  • 2
    except, that doesnt seem to work for , since it is stripped from the result. $.get() does load the body, but doesnt have the page fragment trick. – commonpike Mar 07 '14 at 22:12