'Injecting jQuery not working

So I'm a bit practicing injecting jQuery into different sites to see how my code works and I came across this situation that I can't seem to figure out why this one doesn't want to accept my injection even if it is placed in the header and says successful.

Here's the code I'm trying to inject:

var fileref = document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js");

if (document.getElementsByTagName("head") != undefined)
{
    console.log("Adding jQuery to head");
    document.getElementsByTagName("head")[0].appendChild(fileref);
}
else 
{
    console.log("Adding jQuery to document");
    document.appendChild(fileref);
}

It usually works on different websites but on that website, I am getting this as result, why?

photo of the selector

Photo of chrome console



Solution 1:[1]

If you open the site: https://greenbuyback.com/cell-phones/?limit=all&model=htc without injecting anything you will see the same error in the console.

This is up to the library https://greenbuyback.com/skin/frontend/default/gbb/js/jm.script.js that requires jQuery 1.8.3. Such version of jQuery is included after the jQuery version 1.10.2. It seems a workaround to solve this compatibility issues.

Solution 2:[2]

Yes and no. You need to use .on(). Replace .live() and place correct parameters. Read about how to use .on()

The main difference is thet you need to call from parent and then select current object. Like this

$(document).on('click', '.selector', function() { /* do stuff */ });

in your case selector is

("#ja-mainnav .btn-toggle") 

For some very random and rear rease which still I do not know. By the way, I investigate bug with

$ == undefined or jquery == undefine

Also you can check old .live

if(jQuery && !jQuery.fn.live) {
    jQuery.fn.live = function(evt, func) {
        $('body').on(evt, this.selector, func);
    }
}

from more than two years. what a shame for me. injected jquery have problem. Now here is the first one which at least will help you

function _isJqLoad() {
    return !(typeof jQuery == 'undefined') && !(typeof $ == 'undefined');
}

Before call any jquery even simple document ready you can check and try to inject

if (_isJQLoad()){

     jQuery(document).ready(function ($) {
          // call js
      });
}
else{

   // try to inject or you can do something

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gaetanoM
Solution 2