Auto Resize iFrames - Part 2

Tagged:  

In part 1 of this 2 part series on auto re-sizing iFrames I explained the basic principal of how it would work (you can read part1 here). Today I will give code examples to better demonstrate how you would accomplish this in the real word.

Below is an example of the server side proxy that would be used (this example is written in classic asp [as I needed to use this scripting language for the server that this script was to run on] but this could easily be re-written in your favorite language).

<%@ LANGUAGE=VBScript%>
<% 
            Response.Buffer=True

            Dim MyConnection
            Dim TheURL  

            ' Specifying the URL
            TheURL = Request("url")

            Set MyConnection = Server.CreateObject("Microsoft.XMLHTTP")

            ' Connecting to the URL
            MyConnection.Open "GET", TheURL, False

            ' Sending and getting data
            MyConnection.Send 

            TheData = MyConnection.responseText
            'Set the appropriate content type

            Dim MoreData, MoreData2

            MoreData2 = "<script type='text/javascript'>function Loadcontent(){var iframes = window.parent.document.getElementsByTagName('iframe'); for(var a=0; a</head>"
>"

             MoreData = TheData
           

            MoreData = Replace(TheData,"</head>",MoreData2)

            Response.Write (MoreData)

            Set MyConnection = Nothing     

%>

The below JavaScript is the most important part of the above code (I've formatted it to make it easier to read). In the proxy script it adds this code to the head tag of a page to accomplish the resizing. This code has been tested in IE6 and IE7, but needs a bit of tweaking to work cross browser (it re-sizes the iFrame incorrectly in FireFox, but this is easily fixed and I will post this code when I finish it).

<script type='text/javascript'>
	function Loadcontent()
	{
	var iframes = window.parent.document.getElementsByTagName('iframe');
		for(var a=0; a

So, in order to have an iFrame that re-sizes you would simple call the proxy url from the iFrames src property and set the url parameter (that us passed into the proxy) to the url of the page that you would like to display in the iFrame.

While this iFrame method accomplishes loading content from a different site you could accomplish the same thing using a div element and the XMLHTTPRequest object in JavaScript. Both methods would require the use of a cross-domain proxy (if loading content from a different site). I would probably use the div XMLHTTPRequest element if I am making a new application (as the div element re-sizes very well on it's own). However, if you have an application that already uses iFrames, then this script would be an easy way to make them re-size.

[...] - bookmarked by 6 members originally found by shugotenshineko on 2008-11-16 Auto Resize iFrames - Part 2 http://www.ajaxonomy.com/2008/web-design/auto-resize-iframes-part-2 - bookmarked by 4 members [...]

[...] public links >> xmlhttprequest Auto Resize iFrames - Part 2 First saved by msittig | 9 days ago Safe and Dirty Remote XMLHttpRequest First saved by k8er12 [...]

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <div> <blockquote> <object> <embed> <img> <param>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.