1

For example, open http://camel.apache.org/ , then open the console in Chrome or Firefox, execute the code bellow for jQuery injection:

var jq = document.createElement('script');
jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js";
document.getElementsByTagName('head')[0].appendChild(jq);

Will get error:

Uncaught TypeError: undefined is not a function 

at jQuery.js line 3536:

return readyList.promise( obj ); // 'promise' is an undefined function.
andyf
  • 3,262
  • 3
  • 23
  • 37
  • unforgettably Google don't let some countries to have access to some of its websites, for example if you are in Iran you can't have access to code.google.com or ajax.googleapis.com – Mohammad Kermani Sep 01 '14 at 05:24
  • @Kermani Clearly they were able to get jQuery to download - the error is happening inside the jQuery file while it's executing. – Joe Enos Sep 01 '14 at 05:30
  • @Kermani I can access ajax.googleapis.com – andyf Sep 01 '14 at 05:30
  • Are there other scripts on the page? I would expect this would only happen if some other script is somehow modifying global variables or types in a way that conflicts with jQuery. Javascript can do some funky things if people are not careful, like overwriting prototypes for built-in types or other craziness. I'd suggest taking out the other scripts one-by-one, and seeing if you can track down one which might be causing the problem. – Joe Enos Sep 01 '14 at 05:34
  • @JoeEnos I saw that page is using prototype.js, but I think jQuery can work well with other libraries as jQuery have a jQuery.noConfilct() method – andyf Sep 01 '14 at 05:38
  • @JoeEnos Hi Joe, I find the reason, see my answer. – andyf Sep 03 '14 at 08:38

2 Answers2

0

In that same page if you add:

var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js";
document.getElementsByTagName('head')[0].appendChild(jq);

It will work.

When you use //ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js without https: or http: it automatically tries to get one of them depending on some factors. In this case it tries to get http: but the site only accepts references to https: so it will fail.

Here is a quote from another post:

Using a protocol-independent absolute path:

<img src="//domain.com/img/logo.png"/>

If the browser is viewing an page in SSL through HTTPS, then it'll request that asset with the https protocol, otherwise it'll request it with HTTP.

This prevents that awful "This Page Contains Both Secure and Non-Secure Items" error message in IE, keeping all your asset requests within the same protocol.

Caveat: When used on a <link> or @import for a stylesheet, IE7 and IE8 download the file twice. All other uses, however, are just fine.

Carlos Calla
  • 6,556
  • 2
  • 16
  • 23
  • Hi, I tried your script(in Chrome), but got the same error. – andyf Sep 01 '14 at 05:23
  • Works on mine, sorry. I didn't expect to get a downvote for trying to help other people. – Carlos Calla Sep 01 '14 at 05:30
  • If this was the problem, then jQuery wouldn't have downloaded successfully in the first place. CDNs tend to have the files located at both HTTP and HTTPS, specifically so that you can use the `//ajax.googleapis.com/` syntax and get the secure or non-secure version, to match your current page. – Joe Enos Sep 01 '14 at 05:31
  • What's your browser? It not works for me, but thanks for your help. – andyf Sep 01 '14 at 05:32
  • @JoeEnos sometimes it fails my friend, I have experienced that in some scenarios. – Carlos Calla Sep 01 '14 at 05:32
0

Answer my questions:

Some sites define the prototype of Object, like:

  if (!Object.prototype.extend) {
    Object.prototype.extend = function(){}
  }

It will break jQuery.

Here is similar question Prototyping Object in Javascript breaks jQuery?

And an advice from John Resig : we're looking into for the future, but please don't do it, regardless of the state of jQuery.

It posted on 6 years ago, but seems it will not be solved.

Here is a solution from the bug ticket :

    for (var i in O) {
        if (!O.hasOwnProperty(i)) { continue; }// Add this to all `for...in` loops
        // other code stays unchanged
    }

but this way, you have to create a customized jQuery(so you can not import jQuery from public CDN).

Community
  • 1
  • 1
andyf
  • 3,262
  • 3
  • 23
  • 37