68

I am trying to grab a webpage and load into a bootstrap 2.3.2 popover. So far I have:

$.ajax({
  type: "POST",
  url: "AjaxUpdate/getHtml",
  data: {
    u: 'http://stackoverflow.com'
  },
  dataType: 'html',
  error: function(jqXHR, textStatus, errorThrown) {
    console.log('error');
    console.log(jqXHR, textStatus, errorThrown);
  }
}).done(function(html) {
    console.log(' here is the html ' + html);

    $link = $('<a href="myreference.html" data-html="true" data-bind="popover"' 
            + ' data-content="' + html + '">');
    console.log('$link', $link);
    $(this).html($link);

    // Trigger the popover to open
    $link = $(this).find('a');
    $link.popover("show");

When I activate this code I get the error:

Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined

What is the problem here and how can I fix it?

jsfiddle

t.niese
  • 39,256
  • 9
  • 74
  • 101
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • Good question. Twitter bootstrap 2.3.x does not use fragments at all. Can you reproduce the error in a fiddle? Perhaps you are using some other library, trying to do something with the markup, like react.js or similar? Just theoretically. – davidkonrad Jan 27 '15 at 19:28
  • Can you specify which line the error occurs on? A line number from your own code will be buried in the stack somewhere. – Michael Scheper Mar 08 '17 at 15:40

3 Answers3

103

The reason for the error is the $(this).html($link); in your .done() callback.

this in the callback refers to the [...]object that represents the ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax)[...] and not to the $(".btn.btn-navbar") (Or whatever you expect where it should refer to).

The error is thrown because jQuery will internally call .createDocumentFragment() on the ownerDocument of object you pass with this when you execute $(this).html($link); but in your code the this is not a DOMElement, and does not have a ownerDocument. Because of that ownerDocument is undefined and thats the reason why createDocumentFragment is called on undefined.

You either need to use the context option for your ajax request. Or you need to save a reference to the DOMElement you want to change in a variable that you can access in the callback.

t.niese
  • 39,256
  • 9
  • 74
  • 101
  • 4
    Even though this may be a good answer, it does not explain the mysterious "_Cannot read property 'createDocumentFragment' of undefined_" error. – davidkonrad Jan 27 '15 at 20:28
  • 3
    @davidkonrad edited the answer to add this information – t.niese Jan 27 '15 at 20:30
  • It sounds plausible +1. Twitter bootstrap is heavily jQuery dependent. Good catch! – davidkonrad Jan 27 '15 at 20:31
  • Thank you, could I ask a favour. I'm pretty inexperienced with js. I think I understand how to do the reference fix, but could I ask you to show me how to set the context property in this case using the fiddle as the basis? – user1592380 Jan 27 '15 at 21:34
  • 2
    @user61629 You just need to add the `context : theElementYouWant,` to your `$.ajax()` options (for more details you should look in the docs of [jQuery.ajax](https://api.jquery.com/jquery.ajax/) that I have linked) and then the `this` in the callback will refer to the `theElementYouWant`. I can't make an example as i don't know what element `this` should be ;) – t.niese Jan 27 '15 at 21:38
  • thanks @t.niese had the same issue. this worked `$.ajax({ context: $(this), method: "POST", url: url, data: {leaveid_m: leaveid} }) .done(function( msg ) { $(this).text('loading...'); $('#empdata').remove(); var Summary_m = $(msg).find('#empdata'); $('#empdata2').before(Summary_m); });` – Patrick Mutwiri Oct 12 '15 at 07:07
14

That error occur because this is referring to the ajax object and not on the DOM element, to solve this you can do something like this:

$('form').on('submit', function(){
    var thisForm = this;

    $.ajax({
        url: 'www.example.com',
        data: {}
    }).done(function(result){
        var _html = '<p class="message">' + result + '</p>';
        $(thisForm).find('#resultDiv').html(_html);
    });
});
Kevin Felisilda
  • 141
  • 1
  • 2
1

That error occurs because this usually refers to the ajax object and not on the DOM element, to prevent this assign this in a variable like below:

/* To add add-on-test in cart */
$(document).on("click", ".aish_test", function(){
          var thisButton = this; //Like this assign variable to this
          var array_coming = '<?php echo json_encode($order_summary_array);?>'; //Json Encoding array intersect variable
          //Converting in array
          var array_check = $.parseJSON(array_coming);
          var result = [];

            for(var i in array_check){
                result.push(array_check [i]);
            }

          //Fetching data:
          var test_data_name = $(this).attr("data-name");
          var test_data_price = $(this).attr("data-price");
          var test_data_id = $(this).attr("data-id");
        
         if($.inArray(test_data_id, result) == -1)
            { 
            //static html 
            var static_html = '<tr><td>'+test_data_name+'</td>';
            static_html += '<td>&#163;'+test_data_price+'</td>';
            static_html += '<td><button type="button" class="close remove_add_test" data-id="'+test_data_id+'" data-price="'+test_data_price+'" aria-hidden="true"> <i class="fa fa-trash-o"></i> </button></td></tr>'; 
               /*ajax call*/
            $.ajax(
                    {
                        type: 'POST',
                        url: 'scripts/ajax/index.php',
                        data: {
                            method: 'addtocart',
                            id: test_data_id,
                            name: test_data_name,
                            price: test_data_price
                        },
                        dataType: 'json',
                        success: function(data) {
                            if (data.RESULT == 0) {
                                $(thisButton).text("added to cart"); //Use again like this
                                $(".cart-number").text(data.productcount);
                                $.growl.notice({
                                    title: "Shopping Cart",
                                    message: data.MSG
                                });
                            $('#myTable tr:first-child').after(static_html);
                            } else if (data.RESULT == 2) {
                                $.growl.error({
                                    title: "Shopping Cart",
                                    message: data.MSG
                                });
                            } else {
                                $.growl.error({
                                    title: "Shopping Cart",
                                    message: data.MSG
                                });
                            }
                        }
                    }); 
            }
            else
            {
                $.growl.error({
                                    title: "Shopping Cart",
                                    message: "Already in Cart"
                                });
            }
            
    });