Code to prevent web pages from being embedded in the framework

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.