The method of JS preventing the Web page from being embedded in the IFRAME frame _javascript skill

Source: Internet
Author: User

This article illustrates the method that JS prevents the Web page from being embedded in the IFRAME frame. Share to everyone for your reference, specific as follows:

For example:

<script type= "Text/javascript" >
  if (window!=top)//Determine whether the current Window object is the top object
  top.location.href = Window.location.href; If not, the top object's URL is automatically directed to the URL of the embedded Web page
</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.

So, I'm thinking today, is there a way that my web page can only be embedded in my own frame, not in someone else's frame?

On the surface, the problem is simple. Just make a judgment: whether the current frame is the same as the top-level frame, and if so, a URL redirect.

if (top.location.hostname!= window.location.hostname) {
  top.location.href = window.location.href;
}

But, unexpectedly, it was wrong to write, and could not run at all. Can you see where the mistake is?

Suppose Top.location.hostname is www.111.com, and Window.location.hostname is www.222.com. In other words, 111.com embeds 222.com into its web page. At this time, compare Top.location.hostname!= window.location.hostname will have what result?

The browser prompts the code to be wrong!

Because of their cross-domain (Cross-domain), the browser's security policy does not allow 222.com Web pages to operate 111.com of Web pages, and vice versa. IE calls this error "No permissions". Further, the browser does not even allow you to view the Top.location.hostname, across the domain case, as soon as you see this object, the error is directly reported.

So how should the code be modified?

In fact, this prompts us to see if Top.location.hostname is an error. If the error indicates that a cross-domain exists, the top object is URL-redirected, and if there is no error, there is no cross-domain (or no framework) to take action.

try{
  top.location.hostname;
} catch (e) {
  top.location.href = window.location.href;
}

This is the correct way to write, in IE and Firefox can run correctly. However, the Chrome browser will be wrong, for some reason, in the Cross-domain case, Chrome to Top.location.hostname do not complain!

No way, only for Chrome, plus a piece of supplemental 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, other domain names will not be able to embed your Web page frame.

For more information on JavaScript-related content readers can view the site topics: "JavaScript operation iframe Techniques Summary", "javascript array operation skills Summary", "JavaScript Mathematical calculation Usage Summary", " JavaScript data structure and algorithm skills summary, "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and tips Summary", "JavaScript Error and debugging skills summary" and JavaScript traversal algorithm and techniques summary

I hope this article will help you with JavaScript programming.

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.