if (window!=top) {//Determines whether the current object is a top object
Top.location.href=window.location.href; If not, the URL of the object is automatically directed to the address of the embedded URL
};
Cons: All of your URLs can no longer be infiltrated into the framework included in your own.
if (top.location.hostname! = window.location.hostname) {
Top.location.href = window.location.href;
}
This scenario is possible, and there is a problem that will cause an error in the If judgment-cross-domain.
Top.location.hostname is www.a.com, and Window.location.hostname is www.b.com. In other words, a.com the B.Com into its web page. At this point, compare top.location.hostname ! = Window.location.hostname Browser will prompt code error!
Just check to see if the Top.location.hostname is an error. If the error indicates that there is a cross-domain, the top object is URL-redirected, and if there is no error indicating that there is no cross-domain (or no frame used), no action is taken.
try{
Top.location.hostname;
}
catch (e) {
Top.location.href = window.location.href;
}
This has been done correctly and works correctly in IE and Firefox. However, the Chrome browser will appear error, do not know why, in the cross-domain situation, chrome to Top.location.hostname do not error!
No way, only for Chrome, add a supplementary code.
try{
Top.location.hostname;
if (top.location.hostname! = window.location.hostname) {
Top.location.href =window.location.href;
}
}
catch (e) {
Top.location.href = window.location.href;
}
OK, the upgrade code is complete. In addition to the local domain name, no other domain name can embed your page into the frame.
Original: http://www.ruanyifeng.com/blog/2010/08/anti-frameset_javascript_codes_continued.html
Code to prevent web pages from being embedded in the framework