0

I tried to include another js file in jquery. I have found this exmaple code and tried, but browser console give me this error.

CODE

        $.getScript( "js/balloon.js", function( data, textStatus, jqxhr ) {

          console.log( data ); // Data returned
          console.log( textStatus ); // Success
          console.log( jqxhr.status ); // 200
          console.log( "Load was performed." );
        });

ERROR

XMLHttpRequest cannot load file:///C:/EDIT/swf/js/balloon.js?_=1420941386739. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource.

What mistake I have made, can anyone tell me ? Thanks.

If there is another good example to include js file in js please suggest for me. Thanks again.

UPDATED

I also tried the code bellow but it included before the js included in html, any suggestion to make it included together ?

    var script = document.createElement('script');
    script.src = "js/balloon.js";
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
FeelRightz
  • 2,777
  • 2
  • 38
  • 73
  • If you already have the script in your directory, why do you have to load it in? Can't you just use a `script` tag? – AstroCB Jan 12 '15 at 02:03
  • 4
    Try running your web page through a web server (i.e. `http://127.0.0.1/project/index.html`) rather than running it directly off a web browser. – Ian Jan 12 '15 at 02:03
  • @AstroCB because I have 20+ of js ... I only need one kind of js when my function need it – FeelRightz Jan 12 '15 at 02:07
  • Depending on your situation and on how big the js files are it is often more efficient to combine your js files into one big js file and then cache it. But yeah pretty sure you need to run it from a server for it to work. – joshhunt Jan 12 '15 at 02:50
  • host your code on some server.currently what happening is.you are running the .html file directly from folder withoit hosting.thats why when you try to access the js file protocol is showing as file:// instead of htttp. you can run the same page on Internet Explorer, this will get run properly. – Pankaj Parkar Jan 12 '15 at 03:02

2 Answers2

0

You need to run the script over a server. Also - by default, for security reasons, you can not load js(jsonp) from another domain. Check link.

Community
  • 1
  • 1
inf1ux
  • 283
  • 1
  • 14
0

host your whole folder of code on server.Currently what happening is.you are running the .html file directly from folder without hosting.thats why when you tried to access the js file from html,the protocol is showing as file:// instead of htttp:// you can run the same page on Internet Explorer, this will get run properly. Chrome has some security policies. That is the reason why it is not running on it.

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299