* Most of the dynamic method to load js can be detect
* Once a js is loaded, the urlprotocal will not detect it any more. But the script will be run once more. Even if the script is altered, the web page won't load it any more.
* Take capture will not trigger url protocol
* Take photo:
native_urI will return the photo lib's path such as: "assets-library:\/\/asset\/asset.PNG?id=195E78F1-50CA-42A0-8D3E-D93171A61657&ext=PNG"
<!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->
file_url will save to tempory dir, will trigger the urlprotocol, data_uri will return base64 data, will not trigger url protocol.
//************************************************************************************
Most of the time, Firefox, Safari and Opera work without much effort and differences between the 3. However, throw IE into the mix and you’re in a whole different world.This is helpful if you need to dynamically inject javascript.And, the biggest problem was setting a function that you want executed after the script is loaded.By this article I will provide 4 way to dynamically load external JavaScript.
choice 1. Using document.write
2 | <script language= "javascript" > |
3 | document.write( "<script src='other.js'><\/script>" ); |
Can be detected;
choice 2.Dynamically change the src property value
1 | <script src= '' id= "s1" ></script> |
2 | <script language= "javascript" > |
3 | document.getElementById("s1").src= "other.js" |
Failed to load script without any error, not detect it
choice 3.Dynamically create <script> element
2 | var oHead = document.getElementsByTagName( 'HEAD' ).item(0); |
3 | var oScript= document.createElement( "script" ); |
4 | oScript.type = "text/javascript" ; |
5 | oScript.src= "other.js" ; |
6 | oHead.appendChild( oScript); |
Can be detected;
choice 4. Get JavaScript by using XMLHTTP,then create script object
1 | <script language= "JavaScript" > |
2 | function GetHttpRequest() |
4 | if ( window.XMLHttpRequest ) |
5 | return new XMLHttpRequest() ; |
6 | else if ( window.ActiveXObject ) |
7 | return new ActiveXObject( "MsXml2.XmlHttp" ) ; |
9 | function AjaxPage(sId, url){ |
10 | var oXmlHttp = GetHttpRequest() ; |
11 | oXmlHttp.OnReadyStateChange = function () |
13 | if ( oXmlHttp.readyState == 4 ) |
15 | if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 ) |
17 | IncludeJS( sId, url, oXmlHttp.responseText ); |
21 | alert( 'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ; |
25 | oXmlHttp.open( 'GET' , url, true ); |
28 | function IncludeJS(sId, fileUrl, source) |
30 | if ( ( source != null ) && ( !document.getElementById( sId ) ) ){ |
31 | var oHead = document.getElementsByTagName( 'HEAD' ).item(0); |
32 | var oScript = document.createElement( "script" ); |
33 | oScript.language = "javascript" ; |
34 | oScript.type = "text/javascript" ; |
38 | oHead.appendChild( oScript ); |
41 | AjaxPage( "scrA" , "b.js" ); |
42 | alert( "dynamically load javascript" ); |
43 | alert( "dynamically load a.js and get the variable:" + str ); |
function loadScript(url, callback) {
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState) { //IE
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function () {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
loadScript("https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function () {})