使用本地资产/ HTML /资源时,jQuery的getScript加入不在的Andr​​oid的WebView工作资产、工作、资源、HTML

2023-09-08 00:17:55 作者:你与时光皆薄情

我有一个原生的Andr​​oid应用程序,包含的WebView。我所有的HTML,JS&安培; CSS已经加入到资产文件夹中的Andr​​oid项目,所以我初始化我在Java code像这样的WebView ...

 的WebView web视图=(的WebView)findViewById(R.id.webview);webView.loadUrl(文件:///android_asset/index.htm); 

一切工作正常,直到我需要从使用的JavaScript动态加载额外的js文件jQuery的 getScript加入方法...

  VAR在scriptUrl =脚本/ dynamicScript.js$ .getScript(在scriptUrl,函数(){警报(成功);})    .fail(函数(五){警报(失败:+ JSON.stringify(E));}); 

基本上脚本没有加载,我得到了以下信息:

 失败:{READSTATE:4的responseText:,状态:404,状态文本:错误} 

我也试过在scriptUrl以下备选方案,并得到完全相同的结果和消息:

 /脚本/ dynamicScript.js./scripts/dynamicScript.jsandroid_asset /脚本/ dynamicScript.js../android_asset/scripts/dynamicScript.js文件:///android_asset/scripts/dynamicScript.js 

我想到这可能有一些做的同源政策。任何得到这个工作帮助AP preciated。

编辑:除了

现在我已经还尝试以下替代$ .getScript()。

使用非异步脚本的AJAX调用。这导致完全相同如上...

  $。阿贾克斯({   网址:在scriptUrl,   数据类型:脚本,   成功:函数(){警报(成功); },   错误:功能(E){警报(失败:+ JSON.stringify(e)条); },   异步:真}); 

和还动态插入脚本标记入目。这似乎并不工作(例如,如果我只是在它从未见过dynamicScript.js警报)...

  $(<脚本类型=文/ JavaScript的'SRC ='+在scriptUrl +'>< / SCRIPT>中)。appendTo(头) ; 

我也可能是值得指出的是,以上所有的尝试工作,如果我托管的网站,而不是远程本地资产但这不是一个可行的solutiojn IE它的工作原理,如果我实例如下...

 的WebView web视图=(的WebView)findViewById(R.id.webview);webView.loadUrl(HTTP://remote-server/index.htm); 

解决方案

啊哈我似乎已经sussed它,这个老经典干得不错......

  VAR在scriptUrl =脚本/ dynamicScript.jsVAR头= document.getElementsByTagName(头)[0];脚本=使用document.createElement('脚本');script.type =文/ JavaScript的';script.src =在scriptUrl;script.onload =函数(){警报(成功); };script.onerror =功能(E){警报(失败:+ JSON.stringify(e)条); };head.appendChild(脚本); 

感谢菲利普·菲茨西蒙斯。不知道为什么jQuery脚本追加没做的伎俩。

I've got a native Android app that contains a WebView. All my HTML, js & css has been added to the Android project in the assets folder so I'm initialising my WebView in the Java code like so...

WebView webView = (WebView)findViewById(R.id.webview);
webView.loadUrl("file:///android_asset/index.htm");

Everything works fine until I need to load an additional js file dynamically from JavaScript using the jQuery getScript method...

var scriptUrl = "scripts/dynamicScript.js";
$.getScript(scriptUrl , function() { alert("Success"); })
    .fail(function(e){ alert("failed: " + JSON.stringify(e)); });

Basically the script isn't loading and I get the following message:

failed: {"readState":4,"responseText":"", "status":404, "statusText":"error"}

I've also tried the following alternatives for scriptUrl and get exactly the same result and message:

"/scripts/dynamicScript.js"
"./scripts/dynamicScript.js"
"android_asset/scripts/dynamicScript.js"
"../android_asset/scripts/dynamicScript.js"
"file:///android_asset/scripts/dynamicScript.js"

I expect this might have something to do with the same-origin-policy. Any help getting this working is appreciated.

EDIT: In Addition

I've now also tried the following alternatives to $.getScript().

Using a non-async 'script' ajax call. This results in exactly the same as above...

$.ajax({
   url: scriptUrl, 
   dataType: 'script', 
   success: function() { alert("Success"); },
   error: function(e){ alert("failed: " + JSON.stringify(e)); },
   async: true
});

And also dynamically inserting a script tag into the head. This does not seem to work (for example if I simply have an alert in dynamicScript.js it is never seen)...

$("<script type='text/javascript' src='" + scriptUrl +"'></script>").appendTo("head");

I might also be worth pointing out that ALL of the above attempts work if I am hosting the website remotely instead of as local assets however this isn't a feasible solutiojn I.E it works if I instantiate as follows...

WebView webView = (WebView)findViewById(R.id.webview);
webView.loadUrl("http://remote-server/index.htm");

解决方案

Aha I seemed to have sussed it, this old classic gets the job done...

var scriptUrl = "scripts/dynamicScript.js";
var head = document.getElementsByTagName("head")[0];
script = document.createElement('script');
script.type = 'text/javascript';
script.src = scriptUrl;
script.onload = function() { alert("Success"); };
script.onerror = function(e) { alert("failed: " + JSON.stringify(e)); };
head.appendChild(script);

Thanks to Phillip Fitzsimmons. Not sure why the jQuery script append didn't do the trick.