`
shappy1978
  • 浏览: 681183 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

NSURLProtocol & JavaScript

 
阅读更多

* 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

1 // you need to put an escape character before the closing </script> tag, like this: <\/script>
2 <script language="javascript">
3 document.write("<script src='other.js'><\/script>");
4 </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"
4 </script>

Failed to load script without any error, not detect it

choice 3.Dynamically create <script> element

1 <script>
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);
7 </script>

Can be detected;

choice 4. Get JavaScript by using XMLHTTP,then create script object

1 <script language="JavaScript">
2 function GetHttpRequest()
3 {
4 if ( window.XMLHttpRequest ) // Gecko
5 return new XMLHttpRequest() ;
6 else if ( window.ActiveXObject ) // IE
7 return new ActiveXObject("MsXml2.XmlHttp") ;
8 }
9 function AjaxPage(sId, url){
10 var oXmlHttp = GetHttpRequest() ;
11 oXmlHttp.OnReadyStateChange = function()
12 {
13 if ( oXmlHttp.readyState == 4 )
14 {
15 if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 )
16 {
17 IncludeJS( sId, url, oXmlHttp.responseText );
18 }
19 else
20 {
21 alert( 'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ;
22 }
23 }
24 }
25 oXmlHttp.open('GET', url, true);
26 oXmlHttp.send(null);
27 }
28 function IncludeJS(sId, fileUrl, source)
29 {
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";
35 oScript.id = sId;
36 oScript.defer = true;
37 oScript.text = source;
38 oHead.appendChild( oScript );
39 }
40 }
41 AjaxPage( "scrA", "b.js" );
42 alert( "dynamically load javascript");
43 alert( "dynamically load a.js and get the variable:" + str );
44 </script>

 

 

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 () {})

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics