JavaScript implementation prevents web pages from being embedded in frame frames code sharing _javascript Tips

Source: Internet
Author: User

Recently, another rogue behavior has become popular in the country: use frame to embed your Web page in its Web page.
For example, there is a website claiming to be a "word of mouth aggregation portal", providing the essence of every online forum in the country. But, in fact, it is to use the framework to crawl other people's Web pages, and then add their own ads and stations, which is different from the Pirate booksellers?!

Prevent methods, in

Copy Code code as follows:

</body>

Before adding this code:


Copy Code code as follows:

<script type= "Text/javascript" >
if (top.location!== self.location)
{
Top.location = self.location;
}
</script>


This piece of code is valid. However, there is a problem: after use, no one can embed your Web page into the framework, including yourself.

Is there a way to make my web page can only be embedded in my own frame instead of someone else's frame?

Copy Code code as follows:

<script type= "Text/javascript" >
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;
}
</script>

OK, the upgrade code is complete. In addition to the local domain name, other domain names will not be able to embed your Web page frame. My blog now uses this piece of code.

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.