JQuery - getScript opens in a new tab

795 views Asked by At


i have a "imho" strange problem with the jquery getScript function.

$.getScript('jquery_ui.js', function (){});
// this code works fine

<script type="text/javascript" src="http://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22feeds%22%2C%22version%22%3A%221%22%7D%5D%7D"></script>
// works fine,too

$.getScript('http://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22feeds%22%2C%22version%22%3A%221%22%7D%5D%7D', function (){});
// This code redirect my page.
// But why, oO? This is the same like <script type="text/javascript" src="http://www.google.com/j ...

How can i fix this bug?

Thanks in advance
Peter

4

There are 4 answers

0
HChen On BEST ANSWER

Defining a callback value for each of the modules you are loading should solve the problem. (Note that the value of autoload in the querystring below contains the names of the modules you're loading and the names of your callbacks.)

function maps(){
  console.log("maps loaded");
}
function feeds(){
  console.log("feeds loaded");
}
$.getScript("https://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22maps%22%2C%22version%22%3A%222%22%2C%22callback%22%3A%22maps%22%7D%2C%7B%22name%22%3A%22feeds%22%2C%22version%22%3A%221%22%2C%22callback%22%3A%22feeds%22%7D%5D%7D");
// https://www.google.com/jsapi?autoload={"modules":[{"name":"maps","version":"2","callback":"maps"},{"name":"feeds","version":"1","callback":"feeds"}]}
2
ahmedsafan86 On

this line means that u already included the file!!
why you need to load it in jquery

<script type="text/javascript" src="http://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22feeds%22%2C%22version%22%3A%221%22%7D%5D%7D"></script>
0
Praveen Prasad On
    jQuery.ajax({
        url: 'http://www.google.com/jsapi?autoload={%22modules%22%3A[{%22name%22%3A%22feeds%22%2C%22version%22%3A%221%22}]}',
        dataType: 'script',
        success: function () {
            alert('succeess');
        }
    });

i usually do this to load javascript files, i run above code on my browser and it worked fine.

Also i have created a demo http://jsfiddle.net/jDwYL/

0
McHerbie On

This is an issue with the Google API Loader. I've experienced this problem myself when using deferred loading of their javascript APIs. I was unable to find a solution to the problem and ultimately just used a conventional <script> tag.

I imagine that their javascript code is running this redirect (not sure if it's deliberate or not). You could always dig through their code to find out where the redirect is occurring. (sounds like a lot of wasted time)

Has anyone else had this issue, but found a way around it?