1. 2013. 12. 6. 22:17 개발자료/W3C

1 source is main page (http://a.domain)
2 source is sub page (http://b.domain)

ajax call version
xRequest("/ajax/",{t:"POST",p:"a=b&param=test"});

old browser = hash
future browser = postMessage

1 source
<script>
var x={c:function(d){
    ajax(d.u,{type:d.t,data:d.p,
        onload:function(){
            if(this.readyState==4){
                e({c:d.c,d:this.responseText});
            }
        }
    });
}};
function hash(u,t,d){
if(!t)t=location.hash;
var v=t.match(RegExp(u+"="+"[^"+(d?d:"&")+"]*","ig"));
    if(v)v=decodeURIComponent(v[0].replace(u+"=",""));
    return v;
}
function ajax(u,p){
    var xr=new XMLHttpRequest();
    if(!p)p={};
    if(!p.data)p.data="";
    if(!p.typw)p.type="GET";
    xr.onreadystatechange=p.onload;
    xr.open(p.type,u,true);
    if(p.type=="POST")xr.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
    xr.send(p.data);
    return xr
}
(function(){
if(typeof window.postMessage!="undefined"){
    window.onmessage=function(e){var m=e.data;
        x[m.c](m.d);
    }
}else if("onhashchange" in window){
    window.onhashchange=function(){if(!location.hash)return;
        var h=location.hash.substr(1),i=h.indexOf("}"),e=eval("("+h.substr(0,i+1)+")");
        x[e.c](h.substr(i+1));
        location.hash="";
    }
}else{
    location.replace("waning.htm");
}
}())
function xRequest(url,d,s){
    var ifr=document.querySelector("#xdom"),t=(new Date()).getTime();
    x[t]=function(r){
        alert(r);
    };
    if(typeof window.postMessage!="undefined"){
        ifr.contentWindow.postMessage({c:t,u:url,t:d.type,p:d.data},"http://"+(s?s:"")+document.domain);
    }else if("onhashchange" in window){
        ifr.src="siteb.htm#{c:"+t+",u:'"+url+"',t:'"+d.type+"',p:'"+d.data+"'}";
    }
}
</script>
2 source
<script>
var x={c:function(e,d){
    ajax(d.u,{type:d.t,data:d.p,
        onload:function(){
            if(this.readyState==4){
                e(this.responseText);
            }
        }
    });
}};
function hash(u,t,d){
if(!t)t=location.hash;
var v=t.match(RegExp(u+"="+"[^"+(d?d:"&")+"]*","ig"));
    if(v)v=decodeURIComponent(v[0].replace(u+"=",""));
    return v;
}
function ajax(u,p){
    var xr=new XMLHttpRequest();
    if(!p)p={};
    if(!p.data)p.data="";
    if(!p.typw)p.type="GET";
    xr.onreadystatechange=p.onload;
    xr.open(p.type,u,true);
    if(p.type=="POST")xr.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
    xr.send(p.data);
    return xr
}
(function(){
if(typeof window.postMessage!="undefined"){
    window.onmessage=function(e){var m=e.data;
        if(e.origin!="http://site.domain")return;
        x.c(function(r){
            e.source.postMessage({c:m.c,d:r},e.origin);
        },m);
    }
}else if("onhashchange" in window){
    window.onhashchange=function(){if(!location.hash)return;
        var e=eval("("+location.hash.substr(1)+")");
        x.c(function(r){
            parent.location=document.referrer+"#{c:"+e.c+"}"+r;
        },e);
    }
}else{
    location.replace("waning.htm");
}
}())
</script>


Posted by Nightly Luna
,
® © Tanny Tales
/ rss