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";
6oHead.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{
17IncludeJS( sId, url, oXmlHttp.responseText );
18}
19else
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";
35oScript.id = sId;
36 oScript.defer = true;
37oScript.text = source;
38oHead.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 () {})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值