smugcloud
smugcloud

Reputation: 627

Error in IE when loading jQuery

I have two web pages (let's call them site A and B) that are both using the same externally sourced jQuery. Site B is simply a subdomain of site A. In site A, I declare the javascript as:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Site A has no problems whatsoever.

On site B, I am declaring the jQuery library slightly different but the libary is found and loaded:

`if (typeof jQuery == 'undefined') {
            document.write(unescape("%3Cscript    src='https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js' type='text/javascript'%3E%3C/script%3E"));`

The Problem: In IE 8 and 9, when site B is loading, the Microsoft security error "This page is accessing information that is not under its control. This poses a security risk. Do you want to continue?"

Debugging: After viewing both site A and B in the IE JavaScript debugger, it's clear that A has no problems where as B prompts the user with this alert when the jQuery library has loaded.

Why would loading this library force the prompt on one site, but not the other?

Upvotes: 0

Views: 427

Answers (2)

Joeytje50
Joeytje50

Reputation: 19112

You should really use the DOM instead of using document.write. That will most likely prevent errors like this. Just do it this way:

if (typeof jQuery === 'undefined') {
    var s = document.createElement('script');
    s.src = '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';
    s.type = 'text/javascript';
    document.head.appendChild(s);
}

EDIT: Hardy's answer is probably the right solution to this problem, but the point remains that this is almost always a better way to add elements to your page.

Upvotes: 0

Hardy
Hardy

Reputation: 5631

Change your site B to use http url to google:

Change this:

https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js

to

http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js

You get that message in IE because of that https creates secure connection and your site is not using https (i assume).

Upvotes: 2

Related Questions