0

I am using jQuery 2.2.4 and a small script and the script is making jQuery give me the following error:

TypeError: a is null


...th>0},fa.contains=function(a,b){return(a.ownerDocument||a)!==n&&m(a),t(a,b)},fa....

Console is pointing at "a.ownerDocument"

Now this is the code that's responsible for making the error happen:

$(function() {
        $('[class*="inc:"]').each(function() {
            var match = /inc:(\S+)/.exec(this.className || '');
            match && $(this).inc(unescape(match[1]));
        });
    });

My index.html code is this:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
  <script type="text/javascript" src="inc.js"></script>

</head>
<body>

    <p class="inc:footer.html">This text will be replaced
    with footer.html</p>


</body>
</html>

Here is the full inc.js:

(function($) {
    $.fn.inc = function(url, transform, post, t) {
        return this.length && url ? this.each(function() {
            t = $(this);
            $.ajax({
                url: url,
                success: function(txt, jqXHR, textStatus) {
                    t.html($.isFunction(transform) ? transform(txt, url) : txt);
                    $.isFunction(post) && post(url, jqXHR, textStatus);
                }
            });
        }) : this;
    };

    $(function() {
        $('[class*="inc:"]').each(function() {
            var match = /inc:(\S+)/.exec(this.className || '');
            match && $(this).inc(unescape(match[1]));
        });
    });

})(jQuery);

How can I fix this issue?

2
  • 2
    This is a violent abuse of the class attribute. If it's not for CSS selectors, don't use class! Instead, use something more logical, eg. <p data-include="footer.html">. jQuery can access this with $('[data-include]').each(function() {var elem = $(this), src = elem.data("include"); elem.inc(src);}); Commented Jun 17, 2016 at 10:00
  • @RoryMcCrossan McCrossan, I've added the whole inc.js to the question..thanks Commented Jun 17, 2016 at 10:03

1 Answer 1

1

Use instead

  $(function() {
    $('[class*="inc:"]').each(function() {
        var match = /inc:(\S+)/.exec(this.className || '');
        if(typeOf(match[1]) != "undefined")
          match && $(this).inc(unescape(match[1]));
    });
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.