简体   繁体   English

在jQuery中包含js文件-更新

[英]Include js file in jquery - updated

I tried to include another js file in jquery. 我试图在jquery中包含另一个js文件。 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. XMLHttpRequest无法加载文件:/// C:/EDIT/swf/js/balloon.js?_ = 1420941386739。 Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource. 跨源请求仅支持以下协议方案:http,数据,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. 如果还有另一个在js中包含js文件的好例子,请给我建议。 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 ? 我也尝试了下面的代码,但是它包含在html包含的js之前,是否有建议将其包含在一起?

    var script = document.createElement('script');
    script.src = "js/balloon.js";
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);

You need to run the script over a server. 您需要在服务器上运行脚本。 Also - by default, for security reasons, you can not load js(jsonp) from another domain. 另外-默认情况下,出于安全原因,您不能从其他域加载js(jsonp)。 Check link . 检查链接

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. 将整个代码文件夹托管在服务器上。当前发生的情况是。您直接从文件夹运行.html文件而不进行托管。这就是为什么当您尝试从html访问js文件时,协议显示为file://而不是htttp://的页面,您可以在Internet Explorer上运行同一页面,这将正常运行。 Chrome has some security policies. Chrome浏览器有一些安全政策。 That is the reason why it is not running on it. 这就是它无法在其上运行的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM