Javascript implements cross-origin access to the dom in iframe (1)

Source: Internet
Author: User

This article will focus on discussing the dom in the iframe for JS cross-origin access operations. Many people have always had an idea to operate iframe as they wish. In this way, static pages have the ability to include and require dynamic pages in php, jsp, and asp to achieve unified multi-page layout.

Javascript cross-origin access operation dom in iframe

With the help of Javascript, We can dynamically load the html page content directed to by the src object attribute of the iframe object like the background. This operation requires two pages, one is the page where iframe is located (the Page name is iPage.html), and the other is the iframe property src pointing to the page name: srcPage.html ).

◆IPage.html, dom in <body>:

 
 
  1. <iframeidiframeid=“iId“name=“iName“  
  2. src=“srcPage.html“scrolling=“no“frameborder=“0“> 
  3. </iframe> 
  4.  

Using srcpage.html, <body> In dom:

 
 
  1. <H1> sister's day
  2. <P> breakfast in the morning, lunch appointment, and Karaoke in the afternoon </p>

Next we will discuss how JS operates the above two pages in IE, discuss firefox's practices, and finally provide methods for operating iframe objects compatible with IE and firefox.

I. Content in iframe for Internet Explorer access operations

As we all know, iframe is a non-standard html Tag. It is a multi-layout tag launched by IE browser, and Mozilla also supports this tag. (Gossip, hey)

1. IE obtains it through document. frames ["IframeName"]. For example, we output h1 content in srcpage.html in ipage.html. JS is as follows:

 
 
  1. alert(document.frames["iName"].document.  
  2. getElementsByTagName(‘h1‘)[0].firstChild.data); 

You will find that adding code to the page does not seem to output the desired stuff. Why? I am not clear about this. I just habitually add window. onload and output it. Note: JS Code is written into this event.) can anyone who knows this tell me. Why? After the changes, the Code IE has output, and document. frames in firefox does not have a defined error message:

 
 
  1. window.onload=(function(){  
  2. alert(document.frames["iName"].document.  
  3. getElementsByTagName(‘h1‘)[0].firstChild.data);  
  4. }); 

2. Another method of IE is contentWindow to get it. Code:

 
 
  1. window.onload=(function(){  
  2. variObj=document.getElementById(‘iId‘).contentWindow;  
  3. alert(iObj.document.  
  4. getElementsByTagName(‘h1‘)[0].firstChild.data);  
  5. }); 

This method has passed IE6, IE7, firefox2.0, and firefox3.0 tests. Good thing! Hey. Check the network and find the MozillaFirefoxiframe. contentWindow. focus buffer overflow vulnerability, which may cause script injection attacks.

Later, I heard that such a thing can be prevented in the background, so I am relieved. However, we still hope that the new version of firefox can solve this risk .)

3.modify the content of the h1 title in srcpage.html. Code:

 
 
  1. IObj.doc ument. getElementsByTagName ('h1 ') [0].
  2. InnerHTML = 'I want to become part of her Day ';

Access the nodes through contentWindow is the same as before.

Ii. Access the content in iframe in firefox

Mozilla supports using IFrameElmRef. contentDocument to access the W3C standard of iframe document objects. You can use the following standard to write less document:

 
 
  1. VariObj = document. getElementById ('iid'). contentDocument;
  2. Alert (iObj. getElementsByTagName ('h1 ') [0].
  3. InnerHTML = 'I want to become part of her Day ');
  4. Alert (iObj. getElementsByTagName ('P') [0]. firstChild. data );

The method compatible with the two browsers is now available, that is, the contentWindow method.

Can I operate iframe at will? If you still feel uncomfortable, you can even rewrite the content in iframe.


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.