Js operation Iframe (cross-origin, highly automatic)

Source: Internet
Author: User
Document directory
  • 3. Rewrite the content in iframe

Many people have always had an idea. It would be nice to operate iframe as they wish. In this way, static pages have the include and require implementation equivalent to dynamic pages in the backend, such as php, jsp, and asp.Uniform multi-page layoutCapabilities.

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 of which is the page where iframe is located (Page name: iPage.html), The other page is that the iframe property src points to the page (Page name: srcPage.html).

IPage.html, dom in <body>:

  1. <Iframeid=“iid=name=?iname=src==srcpage.html "scrolling =" no "frameborder =" 0 "> </iframe>

SrcPage.html, dom in <body>:

  1. <H1> sister's day
  2. <P> breakfast in the morning, lunch appointment, Karaoke in the afternoon, and tossing with your brother at night </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:Ipage.html outputs h1 content in srcpage.html. JS is as follows:

  1. Alert (document. frames ["iName" 2.16.doc ument. 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 (Note: JS Code is written into this event). Anyone who knows can 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" 2.16.doc ument. getElementsByTagName ('h1 ') [0]. firstChild. data );
  3. });

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

  1. Window. onload = (function (){
  2. VariObj = document. getElementById ('iid'). contentWindow;
  3. Alert(iObj.doc ument. getElementsByTagName ('h1 ') [0]. firstChild. data );
  4. });

This method has passed ie6, ie7, firefox2.0, and firefox3.0 tests. Good thing! Hey. (Check on the Internet and find the Mozilla Firefox iframe. 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]. innerHTML =' I want to be 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]. innerHTML =' I want to be part of her Day ');
  3. Alert (iObj. getElementsByTagName ('P') [0]. firstChild. data );

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

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

3. Rewrite the content in iframe

You can rewrite the content in iframe through designMode (set the document to editable design mode) and contentEditable (set the content to editable. Code:

  1. VariObj = document. getElementById ('iid'). contentWindow;
  2. IObj.doc ument. designMode = 'on ';
  3. IObj.doc ument. contentEditable = true;
  4. IObj.doc ument. open ();
  5. IObj.doc ument. writeln ('
  6. IObj.doc ument. writeln ('<style> body {background: #000; font-size: 9pt; margin: 2px; padding: 0px ;}</style> ');
  7. IObj.doc ument. writeln ('
  8. IObj.doc ument. close ();

For information on these two objects, see: http://msdn.microsoft.com/en-us/library/ms533720 (VS.85). aspx

Firebug tests the above Code Performance

 

 

Comment out iObj.doc ument. designMode = 'on ';

IObj.doc ument. contentEditable = true;

 

The effect has not changed, and the time efficiency is nearly three times before the annotation. Hey. The two objects refer to the writing of some people on the network and rewrite the content in iframe. In fact, there is no need to use designMode and contentEditable unless there are other requirements.

Iv. iframe adaptive height

With the above principle, it is quite simple to implement this, that is, to set the height value of iframe to the height value of the document in it. Code:

  1. Window. onload = (function (){
  2. Var iObj = document. getElementById ('iid ');
  3. IObj. height = iobj.content?document.doc umentElement. scrollHeight;
  4. });

// JavaScript Document
// ** Iframe automatically adapts to the page **//

// Enter the list of iframe names that you want to automatically adjust the height based on the page height
// Use commas (,) to separate the IDs of each iframe. For example, ["myframe1", "myframe2"] can have only one form.

// Define the iframe ID
Var iframeids = ["main"]

// If your browser does not support iframe, whether to hide iframe: yes, no
Var iframehide = "yes"

Function dyniframesize ()
{
Var dyniframe = new Array ()
For (I = 0; I <iframeids. length; I ++)
{
If (document. getElementById)
{
// Automatically adjust the iframe height
Dyniframe [dyniframe. length] = document. getElementById (iframeids [I]);
If (dyniframe [I] &! Window. opera)
{
Dyniframe [I]. style. display = "block"
If (dyniframe [I]. contentDocument & dyniframe [I]. contentDocument. body. offsetHeight) // if your browser is NetScape
Dyniframe [I]. height = dyniframe [I]. contentDocument. body. offsetHeight;
Else if (dyniframe [I]. Document & dyniframe [I]. Document. body. scrollHeight) // if your browser is IE
Dyniframe [I]. height = dyniframe [I]. Document. body. scrollHeight;
}
}
// Display the browser that does not support iframe Based on the set parameters
If (document. all | document. getElementById) & iframehide = "no ")
{
Var tempobj = document. all? Document. all [iframeids [I]: document. getElementById (iframeids [I])
Tempobj. style. display = "block"
}
}
}

If (window. addEventListener)
Window. addEventListener ("load", dyniframesize, false)
Else if (window. attachEvent)
Window. attachEvent ("onload", dyniframesize)
Else
Window. onload = dyniframesize

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.