.
Showing posts with label HTML Scripts. Show all posts
Showing posts with label HTML Scripts. Show all posts

iframe HTML

Inline Frames are a great implementation of the frames idea — they allow you to open new pages inside main pages, without the many problems brought about by classic-style frames.
<iframe> Basics
Inline or “floating” frames are ones which appear on a page, much like an image or a table would. This allows you to open completely separate pages in the middle of your pages. Here’s a simple example of an inline frame:
So now, I have two pages being displayed on one page, without the restrictions of the usualframeset stuff. The code for inline frames is very easy, and very similar to the <img>element. To get that page to display, I wrote
<iframe src="../examples/inlineframes1.html" width="80%" height="110"> </iframe>
Very, very easy really. The iframe element sets up some space for the new content, and thesrc attribute specifies the address of the inlined file. If you wanted to use a page from another website, you could just put in its full URL, starting with the “http://” part.
The width and height of the frame are denoted as either pixel values or percentages. You doneed a closing </iframe> tag, even though you'd imagine it's un-needed, so don’t forget it. Anything you put between the tags will appear to browsers who can’t do iframes (i.e. Netscape 4).
Interaction
If you have multiple iframes on the same page you can have them interacting, by sending commands between them, just like normal frames. Check out this:
   
Again, this is basically the same method of interlinking you’ve learned in basic frames. You simply give your iframes a name attribute, and then add the appropriate target="name" to the link. So, the code would go something like this:
<iframe src="left.html" name="left"></iframe>
<iframe src="right.html" name="right"></iframe>
Then in left.html, add target="right" to the link. Easy. And if it’s not, you should probably have a look back over the first frames tutorial. If you want to set up this effect, you will need to create a blank html file to sit in the right iframe waiting to change — you cannot leave thesrc empty.
Also note how the Back and Forward buttons in your browser behave. If you click a link that opens in an iframe, pressing Back will make the frame return to its previous contents, and you’ll need to press Back again to go back a page in your history.
All the attributes
There are a load of attributes you can use on your inline frames, and they are:
scrolling="no"
if the framed page is too big for the dimensions you've specified a scrollbar will appear. This attribute will stop this from happening.
frameborder="0"
setting the border to 0 gets rid of it, allowing the page to seamlessly integrate with your main page. Possible values are 1 (yes) and 0 (no), you cannot give it a bigger border.
marginwidth="x"
adds some spacing between the iframe’s side borders and the page inside it.
marginheight="x"
adds some spacing between the iframe’s top and bottom borders and the page inside it. Any value you give marginwidth and marginheight will be added to anymargins you’ve applied to the inner pages themselves.
align="right"
like the image attribute, this will affect how the text around the frame aligns itself.
hspace="x"
sets a margin of white space around the iframe to the sides.
vspace="x"
sets a margin of white space to the top and bottom of the iframe, pushing it away from other page elements.
Most of these attributes can be replaced with appropriate CSS, for borders and spacing.

iFrame HTML Basics


Reference Guide for basic HTML tags for iframes. IFrames are useful for a wide variety of content and especially for in-site navigation where you can use on to load pages or code snippets dynamically on a page without being an AJAX programmer and without using any javascript.
If you combine PHP with JavaScript you can do many things which almost look like AJAX - but are far simpler to implement as the only knowledge you would need is HTML and just a little bit of PHP for more advanced scripts.

Basic IFrame HTML

Bare minimum iframe code:

iframe code
  1. Opening tag
  2. iframe page source
  3. height of the iframe
  4. width of the iframe
  5. Non-iframe content (What to display in the users' browser if they are not capable of viewing iframes.)
  6. Closing tag 


Sample iFrame HTML

Below is a demonstration of two iframes. One with frameborders, and the other without. You can dynamically load different content by clicking on the two links below each frame. Page-2 is with a colored background so that you can easily see the boundaries of the iframe in the borderless example.
To direct a link to open inside of an iframe, the code is simple. You merely name the iframe, and then set the link as shown below. We have named our iframe "test". We then set the link to open in target="test".

Iframe with frameborders

Code:


HTML <iframe>

The HTML <iframe> tag represents a nested browsing context in an HTML document.
The <iframe> tag allows you to embed another document within the current HTML document. It also allows you to provide a nested browsing context without using another document - by simply passing the content to the <iframe> via thesrcdoc attribute.
Prior to HTML5, the <iframe> tag was said to create an inline frame. The HTML5 specification does not use the term inline frame, but uses nested browsing context instead.
Syntax
The <iframe> tag is written as <iframe></iframe> with the applicable attributes inserted into the start tag.
Like this:
<iframe src=""> </iframe>
Or using the srcdoc attribute:
<iframe srcdoc=""> </iframe>
Using the src, width, height, and seamlessattributes:
<iframe src="" width="" height="" seamless> </iframe>
Examples
Basic tag usage
The following example uses the src attribute to specify an external document that the nested browsing context is to contain. In this example, we don't use the width or height attribute.
Refresh Result 
1
<iframe src="/html/tags/html_iframe_tag_example.cfm"></iframe>
The width and heightAttributes
Here, we add the width and height attributes to the previous example.
Refresh Result 
1
<iframe src="/html/tags/html_iframe_tag_example.cfm" width="150" height="150"></iframe>
The seamless Attribute
The WHATWG HTML Living Standard and HTML 5.1 introduced the seamless attribute. The seamlessattribute specifies that the element's browsing context is to be rendered in a manner that makes it appear to be part of the containing document (seamlessly included in the parent document). This results in the document's styles being applied to the nested content. It also results in any links being opened in the <iframe> element's parent browsing context.
Note that at the time of writing, browser support for the seamless attribute is poor.
Refresh Result 
1
<iframe src="/html/tags/html_iframe_tag_example.cfm" width="150" height="150" seamless></iframe>
The srcdoc Attribute
You can use the srcdoc attribute to specify the content that should appear in the nested browsing context.
Note that <!doctype> and <title> are optional when using the srcdoc attribute within a<iframe> tag (they are required on most other HTML documents).
Refresh Result 
1
<iframe srcdoc="
2
<html>
3
<head>
4
</head>
5
<body>
6
<p>Nested browsing context using the <code>srcdoc</code> attribute.</p>
7
</body>
8
</html>
9
"
10
width="150" height="150" seamless></iframe>
Differences Between HTML 4 & HTML 5
HTML5 introduced the following attributes:
srcdocsandbox
Both HTML 5.1 and the HTML Living Standard have introduced the following attributes:
seamlessallowfullscreen
HTML5 does not support the following attributes (which were supported in HTML 4 and earlier):
alignlongdescframeborderscrollingmarginwidthmarginheight
Also, in HTML 4 and previous versions you could create "fallback content" by placing content within the opening and closing <iframe> </iframe>tags. In HTML5 however, the <iframe> element never has fallback content.
To see more detail on the HTML 4 and HTML5 versions see HTML5 <iframe> Tag and HTML4<iframe> Tag. Also check out the links to the official specifications below.
Template
Here's a template for the <iframe> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.
For more information on attributes for this tag, seeHTML5 <iframe> Tag and HTML4 <iframe> Tag.
<iframe src="" srcdoc="" name="" sandbox="" width="" height="" accesskey="" class="" contenteditable="" contextmenu="" dir="" draggable="" dropzone="" hidden="" id="" itemid="" itemprop="" itemref="" itemscope="" itemtype="" lang="" spellcheck="" style="" tabindex="" title="" translate="" onabort="" onautocomplete="" onautocompleteerror="" onblur="" oncancel="" oncanplay="" oncanplaythrough="" onchange="" onclick="" onclose="" oncontextmenu="" oncuechange="" ondblclick="" ondrag="" ondragend="" ondragenter="" ondragexit="" ondragleave="" ondragover="" ondragstart="" ondrop="" ondurationchange="" onemptied="" onended="" onerror="" onfocus="" oninput="" oninvalid="" onkeydown="" onkeypress="" onkeyup="" onload="" onloadeddata="" onloadedmetadata="" onloadstart="" onmousedown="" onmouseenter="" onmouseleave="" onmousemove="" onmouseout="" onmouseover="" onmouseup="" onmousewheel="" onpause="" onplay="" onplaying="" onprogress="" onratechange="" onreset="" onresize="" onscroll="" onseeked="" onseeking="" onselect="" onshow="" onsort="" onstalled="" onsubmit="" onsuspend="" ontimeupdate="" ontoggle="" onvolumechange="" onwaiting="" > </iframe>

Auto-resize Iframe when Content Size Changes

An iframe is a section of a web page where the content of another web page can be published. It's done with an HTML iframe tag.
This article describes how to resize the height of an iframe automatically — whenever the content of the web page being published within the iframe changes. (Resizing the width can be accomplished by substituting each word "height" with "width", maintaining the letter case.)
Of course, it's fairly simple to adjust the height of an iframe when the content first loads. (I'll show you how to do that, too.) But if the content in the iframe changes, the iframe size may need to be adjusted further.
A common use of an iframe is to present a form, so a page reload is unnecessary when the form submits. After the form is submitted, a thank-you page is displayed within the iframe.
The thank-you page generally is a different size, however, and either puts scrollbars into the iframe or leaves a large blank area within the iframe.
Therefore, the iframe size needs to be adjusted.
(There are other reasons that content changes within an iframe. The same principle described in this article can be used for other implementations.)
A caveat: The URL of the page being published within the iframe must be on the same domain as the web page with the iframe tag. Otherwise, the web page with the iframe tag won't be able to determine the size of the content within the iframe. It's subject to the built-in same-origin security restriction.
Here's an example iframe that resizes for the thank-you page after the form button is clicked:
Before describing how to implement the iframe auto-resize for your site, here's the code for the above example. There are 3 files.
File 1: The web page containing the iframe tag needs to contain this code:
<div style="border:2px solid #666; border-radius:11px; padding:20px;"> <iframe id="form-iframe" src="iframe1form.html" style="margin:0; width:100%; height:150px; border:none; overflow:hidden;" scrolling="no" onload="AdjustIframeHeightOnLoad()"></iframe> <script type="text/javascript"> function AdjustIframeHeightOnLoad() { document.getElementById("form-iframe").style.height = document.getElementById("form-iframe").contentWindow.document.body.scrollHeight + "px"; } function AdjustIframeHeight(i) { document.getElementById("form-iframe").style.height = parseInt(i) + "px"; } </script> </div>
The purpose of the div tag is merely to provide a border with rounded corners for the iframe. At least some browsers don't implement iframe borders with as much versatility as a div tag.
File 2: The web page (iframe1form.html) containing the form:
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>iFrame Resizing Test</title> </head> <body style="margin-top:0; text-align:center;"> <h1 style="margin-top:0;"> iFrame Resizing Test </h1> <p> When the form button is clicked, the thank-you page loads. </p> <form method="post" action="iframe1th.html"> <input type="submit" value="Click"> </form> </body> </html>
File 3: The web page (iframe1th.html) that is the thank-you page:
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>iFrame Test Thank-you Page</title> </head> <body style="margin-top:0;"> <div id="page-container"> <h1 style="margin-top:0; text-align:center; letter-spacing:1px;"> Thank You! </h1> </div> <script type="text/javascript"> parent.AdjustIframeHeight(document.getElementById("page-container").scrollHeight); </script> </body> </html>
Put all three files into the same directory on your server. Type the URL of file 1 into your browser. You should see the same functionality as the above example, iFrame Resizing Test.
The iframe is resized as its content changes.


Here's an overview of how it works:
When the web page with the iframe tag loads, file iframe1form.html (the page with the form) is loaded into the iframe tag.
When the button in file iframe1form.html is clicked, file iframe1th.html (the thank-you page) is loaded into the iframe tag, replacing the page with the form.
When file thank-you page loads into the iframe, it tells the web page with the iframe tag how tall the iframe shall be to contain all of the thank-you page's content.
Now, let's talk about implementing a similar system on your website.
Implementing an Auto-resizing Iframe with a Form
Let's get the pages ready last to first — the thank-you page, the form page, then the page with the iframe tag. The reason for doing them in backward order is so the location of a file is known before it needs to be specified.
A. The Thank-you Page
Remove the top margin from the web page.
For the JavaScript to calculate the height required for the iframe, the top margin of the web page needs to be zero. In the above example code, you'll see the body tag has margin-top:0; inline CSS. Alternatively, removing the top margin from the body tag can be done in a style sheet.
Only the top margin is required to be removed. But all margins can be removed safely.
If a top margin remained in place, the iframe's height calculation would be off by the amount of the margin at least in some browsers.
Remove the top margin of the first line of content.
Similar to the top margin of the page, any top margin in the first line of content will cause the iframe's height calculation by the amount of the margin. A style="margin-top:0;" inline CSS attribute should do the trick. See the h1 tags of the example files.
Put the thank-you page content into a div.
Example div tag (must have an id value):
<div id="page-container">
The reason for the div tag is for the JavaScript (next item on the list) to be able to calculate the height of the content. So, put the div tag as the first line below the body tag and the closing </div> tag immediately above the closing </body> tag.
Put JavaScript at the bottom of the body content.
Immediately below the closing </div> tag (see previous step), insert this JavaScript:
<script type="text/javascript"> parent.AdjustIframeHeight(document.getElementById("page-container").scrollHeight); </script>
If the id value of the div (previous list item) isn't "page-container", then"page-container" in the JavaScript needs to be changed accordingly.
The JavaScript runs immediately after the div is loaded. When it runs, it reports the div's content height to JavaScript located on the web page with the iframe tag — which then adjusts the iframe height.
Upload the thank-you page to your server and make a note of its URL.
B. The Page with the Form
(The first two items on this list are identical to the first two item on the thank-you page list.)
Remove the top margin from the web page.
For the JavaScript to calculate the height required for the iframe, the top margin of the web page needs to be zero. In the above example code, you'll see the body tag has margin-top:0; inline CSS. Alternatively, removing the top margin from the body tag can be done in a style sheet.
Only the top margin is required to be removed. But all margins can be removed safely.
If a top margin remained in place, the iframe's height calculation would be off by the amount of the margin at least in some browsers.
Remove the top margin of the first line of content.
Similar to the top margin of the page, any top margin in the first line of content will cause the iframe's height calculation by the amount of the margin. A style="margin-top:0;" inline CSS attribute should do the trick. See the h1 tags of the example files.
Specify the URL of the thank-you page as the value for the form's action attribute.
The thank-you page was uploaded to the server in step A.
Upload the page with the form to your server and make a note of its URL.
C. The Web Page with the Iframe Tag
Insert the iframe tag into the web page.
Here's an example:
<iframe id="form-iframe" src="iframe1form.html" style="margin:0; width:100%; height:150px; border:none; overflow:hidden;" scrolling="no" onload="AdjustIframeHeightOnLoad()"></iframe>
Change the value of the src attribute to the URL of the page with the form uploaded to the server in step B.
Both the id and onload attributes are required. The style may be changed as appropriate for your implementation.
The id value is used in the JavaScript (next list item) and onload attributes runs the JavaScript to adjust the iframe height as soon as the iframe finishes loading.
Insert JavaScript below the iframe.
Put this JavaScript somewhere below the iframe. It may be down at the end of the page and/or imported from an external file.
<script type="text/javascript"> function AdjustIframeHeightOnLoad() { document.getElementById("form-iframe").style.height = document.getElementById("form-iframe").contentWindow.document.body.scrollHeight + "px"; } function AdjustIframeHeight(i) { document.getElementById("form-iframe").style.height = parseInt(i) + "px"; } </script>
The id value of the iframe tag in the previous list item is specified in the JavaScript in three places. If the id value"form-iframe" of the iframe is changed, all three places in the JavaScript must be changed accordingly.
The function AdjustIframeHeightOnLoad() adjusts the iframe height when the iframe is first loaded. The function AdjustIframeHeight() adjusts the iframe height when called from the JavaScript in the thank-you page.
Upload the page with the iframe tag to your server and make a note of its URL.
To test your installation, type the URL of the page with the iframe tag into your browser.
Once implemented, doing form submissions in an iframe lets the entire submission-to-confirmation process take place without reloading the web page. The iframe is resized as its content changes.