functions - Scripts not appending to <head> element in AJAX call - why?

admin2025-01-08  3

I'm trying (and failing), to get scripts to load in the header of certain posts before the rest of the page loads. Some of my posts have embeds that require scripts to work, but my makeshift code below is not working properly.

This AJAX call is successfully being sent, the scripts are being filtered from the received data, but for some reason, my AJAX call is not appending these scripts to my head element in my HTML code. So where have I gone wrong?

(function ($) {
    $(document).ready(function () {

      var $mainContent = $(".main");
      var value;
      $this = '';

      $(document).on("click", "#posts a", function (e) {
        e.preventDefault();
        $this = $(this);
        value = $this.attr("href");

          $.ajax({
            url: value,
            type: 'POST',
            dataType: "html",
            error: function (data) {
              console.warn(data);
            },
            success: function (data) {
              var dom = $(data);
              dom.filter("script").each(function () {
                if (this.src) {
                  var script = document.createElement('script'),
                    i, attrName, attrValue, attrs = this.attributes;
                  for (i = 0; i < attrs.length; i++) {
                    attrName = attrs[i].name;
                    attrValue = attrs[i].value;
                    script[attrName] = attrValue;
                  }
                  document.getElementsByTagName("head")[0].appendChild(script);
                }
              });
            }
          }).done(function (data) {
            var dom = $(data);
            $mainContent.html(dom.find('.main').html());
          });
      });
    });
  })(jQuery);

I'm trying (and failing), to get scripts to load in the header of certain posts before the rest of the page loads. Some of my posts have embeds that require scripts to work, but my makeshift code below is not working properly.

This AJAX call is successfully being sent, the scripts are being filtered from the received data, but for some reason, my AJAX call is not appending these scripts to my head element in my HTML code. So where have I gone wrong?

(function ($) {
    $(document).ready(function () {

      var $mainContent = $(".main");
      var value;
      $this = '';

      $(document).on("click", "#posts a", function (e) {
        e.preventDefault();
        $this = $(this);
        value = $this.attr("href");

          $.ajax({
            url: value,
            type: 'POST',
            dataType: "html",
            error: function (data) {
              console.warn(data);
            },
            success: function (data) {
              var dom = $(data);
              dom.filter("script").each(function () {
                if (this.src) {
                  var script = document.createElement('script'),
                    i, attrName, attrValue, attrs = this.attributes;
                  for (i = 0; i < attrs.length; i++) {
                    attrName = attrs[i].name;
                    attrValue = attrs[i].value;
                    script[attrName] = attrValue;
                  }
                  document.getElementsByTagName("head")[0].appendChild(script);
                }
              });
            }
          }).done(function (data) {
            var dom = $(data);
            $mainContent.html(dom.find('.main').html());
          });
      });
    });
  })(jQuery);
Share Improve this question asked Jun 13, 2020 at 22:17 SiddersSidders 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This code worked for me:

    success: function (data) {
      var dom = $(data);
      scripts = dom.filter('script').each(function () {
        script = $(this);
        $("head").append(script);
      });
      //...more code
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266038a1088.html

最新回复(0)