13

I load a html with ajax. I want to load the result in a jquery object. I tried that but it returns null. How can I do this? I got a complete page including doctype, head elements and body elements.

var test = $(result); //result contains html code
alert(test.html()); //returns null

I load the data with this function.

function ajaxLoadContent(element) {
    $.ajax({
        url: "url to the page",
        type: "GET",
        timeout: 5000,
        datattype: "html",
        success: function(result) {
        //handler
        },
    });
    return false;
Mark Baijens
  • 13,028
  • 11
  • 47
  • 73
  • result is probably just a string – hunter Dec 13 '10 at 14:40
  • Can you show how you get the data into `result`? Also, does `alert(result);` show the html in the alert? Just dumping html into a variable like that doesn't automatically make the .html() function return html. your result variable may already be html. – Josh Dec 13 '10 at 14:41
  • 1
    Why not just print out the result? This should be text. – WaiLam Dec 13 '10 at 14:41
  • @Josh, added the function that call the page to the post. Yes the html is shown in the alert when i do `alert(result)`. – Mark Baijens Dec 13 '10 at 14:50
  • @WaiLam I want to do some modification before i use the html on my page. – Mark Baijens Dec 13 '10 at 14:51

4 Answers4

56

It's a while ago, but maybe you're still interested in it..

The intern implementation of $(String) is not able to build an jQuery object that contains head or body tags. It will simply ignore them and move all elements inside on level up.

So if your string is for example

<html>
  <head>
    <meta ...>
  </head>
  <body>
    <div id="a"/>
  </body>
</html>

the resulting jQuery object will be an array of two elements

[<meta ...>, <div id="a" />]

to get a body-like jQuery object cut everything but the body content before passing it to jQuery:

body = '<div id="body-mock">' + html.replace(/^[\s\S]*<body.*?>|<\/body>[\s\S]*$/ig, '') + '</div>';
var $body = $(body);

now things work as expected.. for example

$body.find('#a')

Hope that helps..

sp00m
  • 47,968
  • 31
  • 142
  • 252
lrsjng
  • 2,615
  • 1
  • 19
  • 23
2

test is just an html string, so you could simply do this to show the contents

alert(result);

and if you want to bind that to an element

$("#myDiv").html(result);
hunter
  • 62,308
  • 19
  • 113
  • 113
  • I want to be able to use some jquery functionality on the return value. – Mark Baijens Dec 13 '10 at 14:52
  • 1
    since result is just a string you will probably need to place it on the page before you can interact with it with jQuery. – hunter Dec 13 '10 at 14:56
  • Mzz than I need to place it in an hidden iframe I guess. That would be nasty. Probably better to design some out of the box solution. – Mark Baijens Dec 13 '10 at 15:00
  • Are you trying to load html and totally overwrite the html on the page? If so, why don't you just navigate to that page? ;) – hunter Dec 13 '10 at 15:17
  • Let me know exactly what you want to do with the HTML once it comes back and I might be able to help you out – hunter Dec 13 '10 at 15:18
  • Well i'm using Ajax for some of my functionality. I got a non JavaScript backup to support non JavaScript users as well. Both call the same url because I don't want to change my content twice when modifications are made. I guess best thing to do is to change my server side code that detects if this is a ajax call or not and modify the return value based on that. – Mark Baijens Dec 13 '10 at 15:24
1
function ajaxLoadContent(element) {
$.ajax({
    url: "url to the page",
    type: "GET",
    timeout: 5000,
    datattype: "html",
    success: function(data) {
     var result = $(data);
    },
});
return false;

You should now be able to call the result like this (remember it's not added to the DOM yet):

alert(result.html());

Add to the DOM

result.appendTo("body");

Let me know if this works for you.

halfpastfour.am
  • 5,764
  • 3
  • 44
  • 61
-1

Try the load method of jQuery object: http://api.jquery.com/load/

Load data from the server and place the returned HTML into the matched element.

pawel
  • 35,827
  • 7
  • 56
  • 53
  • The author of this answer might be onto something. Converting the whole HTML string to a `data:` url and calling $.ajax() on it might just be the ONLY reliable method. – Szczepan Hołyszewski Oct 11 '17 at 23:44