JS 크로스 필드 총괄

18839 단어 js
원본:
http://www.cnblogs.com/qixuejia/archive/2012/08/29/2662220.html

javascript       :

1、           , :a.c.com b.c.com

2、         , :www.a.com www.b.com

3、     , :www.a.com:8080 www.a.com:8088

4、    , :http://www.a.com https://www.a.com

 

    3 4,      proxy   ,      :

a、         proxy  

b、    js      proxy  

c、proxy                

d、proxy             js

         :

<form id="form1" runat="server">
    <div>                
        <input type="text" id="txtSrc" value="http://www.gzsums.edu.cn/webclass/html/html_design.html" style="width: 378px" />
        <input id="btnProxy" type="button" value="  Proxy    " onclick="GetDataFromProxy();" /><br />
        <br />
        <br />
    </div>
   <div id="divData"></div> 
</form>
</body>
<script language="javascript"  type="text/javascript">       
   function GetDataFromProxy() {
       var src = document.getElementById('txtSrc').value;
       var request = null;
       if (window.XMLHttpRequest) {
           request = new XMLHttpRequest();
       }
       else if (window.ActiveXObject) {
           request = new ActiveXObject("Microsoft.XMLHTTP");
       }

       request.onreadystatechange = function() {
           var ready = request.readyState;
           var data = null;
           {
               if (ready == 4) {
                   data = request.responseText;
                   document.getElementById('divData').innerHTML = data;
               }
               else {
                   document.getElementById('divData').text = "Loading";
               }
           }
       }

       var url = "Proxy.ashx?src=" + escape(src);        
       request.open("get",url,false);
       request.send(null);
   }     
</script>

   Proxy    :

using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.Text;


namespace WebApplication1
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Proxy : IHttpHandler
    {
        const int BUFFER_SIZE = 8 * 1024;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string src = context.Request["src"];

            WebRequest wr = WebRequest.Create(src);
            WebResponse wres = wr.GetResponse();
            Encoding resEncoding = System.Text.Encoding.GetEncoding("gb2312");
            StreamReader sr = new StreamReader(wres.GetResponseStream(), resEncoding);
            string html = sr.ReadToEnd();
            sr.Close();
            wres.Close();

            context.Response.Write("<br/><br/><br/><br/>");
            context.Response.Write(html);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

   1 2      proxy     ,    7      :

1、document.domain+iframe(      1):

    a、              document.domain,           (window.location.hostname)

  b、             iframe,iframe        

    c、        ,  iframe.contentDocument || iframe.contentWindow.document           

    d、                      

        ,          ,            。

         :

<body>
<div>                
    <input type="text" id="txtSrc" value="http://b.a.com/DomainTest2.htm" style="width: 378px" />
    <input id="btnDomain" type="button" value="  Domain    " onclick="GetDataFromDomain();" /><br />
    <br />
    <br />
</div>
<div id="divData"></div> 
</body>
<script language="javascript"  type="text/javascript">
    document.domain = 'a.com';
    var src = document.getElementById('txtSrc').value;
    var ifr = document.createElement('iframe');
    ifr.src = src;
    ifr.style.display = 'none';
    document.body.appendChild(ifr);
    function GetDataFromDomain() {
        var doc = ifr.contentDocument || ifr.contentWindow.document;
        alert(doc.getElementById("data").value);
    }
</script>

         :

<body>
    <input type="hidden" id="data" value="Cross Domain" style="width: 378px" />
</body>
<script language="javascript"  type="text/javascript">
    document.domain = 'a.com';    
</script>

2、    script:

  a、            script,script URL            (  ),      javascript      ,  URL         ,      GET      。

  b、   script       js            

          :

<head>
    <title>Script Test</title>
    <script language="javascript" type="text/javascript">
    function load_script(callback){   
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        var src = document.getElementById('txtSrc').value;
        script.type = 'text/javascript';   
        script.src = src;   
        //   jQuery script       
        script.onload = script.onreadystatechange = function(){   
            if((!this.readyState||this.readyState === "loaded"||this.readyState === "complete")){   
                callback && callback();   
                // Handle memory leak in IE   
                script.onload = script.onreadystatechange = null;   
                if ( head && script.parentNode ) {   
                  head.removeChild( script );   
                }   
            }   
        };   
        // Use insertBefore instead of appendChild  to circumvent an IE6 bug.   
        head.insertBefore( script, head.firstChild );
    }    
    </script>
</head>
<body>
<input type="text" id="txtSrc" value="http://www.b.com/scripttest.aspx" style="width: 378px" />
<input type="button" value="      script       " onclick="load_script(function(){alert('    script    ')})"/>
</body>

           :

protected void Page_Load(object sender, EventArgs e)
{            
            Response.Clear();
            Response.ContentType = "application/x-javascript";
            Response.Write(String.Format(@"alert('{0}');", DateTime.Now));
            Response.End();
}

3、location.hash+iframe:

  a、          iframe,iframe          ,         hash      

  b、          ,       location.hash       

    c、          iframe,iframe                 ,                             hash    

    d、          ,       location.hash       

    e、           ,       location.hash           hash 

www.a.com/a.html#aaa,  #aaa  location.hash 

         :

<body>
<div>
<input type="text" id="txtSrc" value="1" style="width: 378px" />
<input id="btnAddHash" type="button" value="  Hash " onclick="addHash();" />
<iframe id="ifr1" style="display:none"></iframe>
</div>
</body>
<script language="javascript"  type="text/javascript">
    function addHash() {
        var src = document.getElementById('txtSrc').value;
        if (src.length > 0) {            
            changeHash(src);
        }
    }
    function changeHash(src) {
        if (document.getElementById('ifr1')) {
            var ifr = document.getElementById('ifr1');
            ifr.src = 'http://www.b.com/Test/HashTest2.htm#' + src;
        }
        else {
            var ifr = document.createElement('iframe');
            ifr.setAttribute('id', 'ifr1');
            ifr.src = 'http://www.b.com/Test/HashTest2.htm#' + src;
            ifr.style.display = 'none';
            document.body.appendChild(ifr);
        }
    }
    function checkHash() {
        if (location.hash && location.hash.length > 1) {
            changeHash(location.hash.substring(1));
        }
    }
    setInterval(checkHash, 2000);
</script>

         :

<body>
<iframe id="ifr2" style="display:none"></iframe>
</body>
<script language="javascript"  type="text/javascript">    
    function checkHash() {
        if (location.hash && location.hash.length > 1) {
            var hashData = location.hash.substring(1);
            var ifr = null;
            if (document.getElementById('ifr2')) {
                ifr = document.getElementById('ifr2');                
            }
            else {
                ifr = document.createElement('iframe');
                ifr.setAttribute('id', 'ifr2');                
                ifr.style.display = 'none';
                document.body.appendChild(ifr);
            }
            switch (hashData) {
                case '1':
                    alert('One');
                    if (ifr) {
                        ifr.src = 'http://www.a.com/test/HashTest3.htm#2';
                    }
                    break;
                case '2':
                    alert('Two');
                    if (ifr) {
                        ifr.src = 'http://www.a.com/test/HashTest3.htm#1';
                    }
                    break;
                default:
                    break;
            }
        }
    }
    setInterval(checkHash, 2000);
</script>

              :

<body></body>
<script language="javascript" type="text/javascript">
    function checkHash() {        
        if (parent && parent.parent && parent.parent.location && self.location.hash.length > 1) {
            parent.parent.location.hash = self.location.hash.substring(1);
        }
    }
    setInterval(checkHash, 500);
</script>

4、window.name:

    a、            iframe,          

    b、          script          window.name 

    c、    iframe onload    iframe                  (              window.name  )

    d、  window.name  (  iframe     ,  window.name     )

window.name        2MB  

         :

<body>
<div>
<input id="btnName" type="button" value="  window.name    " onclick="getData();" />
<iframe id="ifr1" style="display:none" src="http://www.b.com/Test/NameTest2.htm"></iframe>
</div>
</body>
<script language="javascript"  type="text/javascript">
    var ischanged = false;
    function changeSrc() {
        if (document.getElementById('ifr1')) {
            var ifr = document.getElementById('ifr1');
            if (!ischanged) {
                ischanged = true;
                ifr.contentWindow.location = 'http://www.a.com/Test/NameTest3.htm';
            }
            else {
                var data = ifr.contentWindow.name;
                alert(data);
            }
        }
        else {
            var ifr = document.createElement('iframe');
            ifr.setAttribute('id', 'ifr1');
            ifr.src = 'http://www.b.com/Test/NameTest2.htm';
            ifr.style.display = 'none';
            document.body.appendChild(ifr);
        }
    }
    function getData() {
        setInterval(changeSrc, 2000);
    }
</script>

         :

<body></body>
<script language="javascript"  type="text/javascript">
    window.name = 'NameTest2';
</script>

              :

<body></body>

(        )

5、HTML5 postMessage

    a、receiverWindow.postMessage(msg, targetOrigin),receiverWindow        window   ,   iframe contentWindow/window.open    /window.frames    ;msg        ,string  ;targetOrigin    receiverWindow URI,        ,  “*”     ,               ,             ,  targetOrigin URI receiverWindow   ,       。

    b、     message       ,    event.origin            event.data          ,event.source       window  

          :

<body>
<div>
<input id="btnPostMessage" type="button" value="  PostMessage    " onclick="getData();" />
<iframe id="ifr" style="display:none" src="http://www.b.com/Test/PostMessageTest2.htm"></iframe>
</div>
</body>
<script language="javascript" type="text/javascript">
    function getData() {
        var ifr = document.getElementById('ifr');
        var targetOrigin = 'http://www.b.com';
        if (ifr.contentWindow.postMessage) {
            ifr.contentWindow.postMessage('PostMessageTest2', targetOrigin);
        }
    }
</script>

         :


<body></body>
<script language="javascript" type="text/javascript">
    window.addEventListener('message', function(event) {
        if (event.origin == 'http://www.a.com') {
            alert(event.data);    
            alert(event.source);    
        }
    }, false);
</script>

6、window.opener(   IE6、7,   operner hack  ,            ,           .kb2497640     )

  a、            iframe,          

    b、       iframe.contentWindow.opener = {a: function(params){...}, b: function(params){...} ...}             

    c、       window.opener.a/window.opener.b           

    d、       parent.opener = {c: function(params){...}, d: function(params){...} ...}             

    e、       opener.c/opener.d           

        opener  

         :

<body>
<iframe id="ifr" src="http://www.b.com/test/OpenerTest2.htm" style="display:none"></iframe>
</body>
<script language="javascript"  type="text/javascript">   
        var ifr = document.getElementById('ifr');
        ifr.contentWindow.opener = { a: function(msg) { alert('    a       :' + msg); } }        
</script>

         :

<body>
</body>
<script language="javascript"  type="text/javascript">
    window.opener.a('aaa'); 
</script>

7、window.navigator(   IE6、7,       ,      )

    a、            iframe,          

    b、       window.navigator.a = function(params){...}; window.navigator.b = function(params){...};             

    c、       window.navigator.a(params); window.navigator.b(params);           

    d、       window.navigator.c = function(params){...}; window.navigator.d = function(params){...};             

    e、       window.navigator.c(params); window.navigator.d(params);           

         :

<body>
<iframe id="ifr" src="http://www.b.com/test/NavigatorTest2.htm" style="display:none"></iframe>
</body>
<script language="javascript"  type="text/javascript">
    window.navigator.a = function(msg) { alert('    a       :' + msg); }
    window.navigator.b = function(msg) { alert('    b       :' + msg); }
    setInterval(function() { window.navigator.c('ccc'); }, 2000);
    setInterval(function() { window.navigator.d('ddd'); }, 2000);
</script>

         :

<body>
</body>
<script language="javascript"  type="text/javascript">
    window.navigator.c = function(msg) { alert('    c       :' + msg); }
    window.navigator.d = function(msg) { alert('    d       :' + msg); }
    setInterval(function() { window.navigator.a('aaa'); }, 2000);
    setInterval(function() { window.navigator.b('bbb'); }, 2000);
</script>



좋은 웹페이지 즐겨찾기