Sticky PostingsWelcome to the SCV DotNet Users GroupMonday, October 27. 2008Autofitting iFrameI presented this technique at the SoCal Code Camp, on 10/26/08.Some errors here were corrected 11/12/08. All code-fragments and files have been updated.IntroductionDoes your iFrame step through multiple pages of varying heights? Or, does the height of the remote page change frequently? Do you find that the height of your iFrame is sometimes too short, cutting off the bottom of the remote page? Is your iFrame sometimes too tall, pushing down your content far beneath the embedded page? Then this technique is for you.Look here for an example. Fill out the survey to see the autofit in action. Each page of the survey inside the iFrame is a different height. Watch the iFrame resize to accommodate each page: http://laptopacademy.net/wordpress/signup This technique requires that you are able to edit the remote web-page. It requires a bit of script, and you’ll need to upload a couple of files to your web-host. ExplanationNormally, the parent page is unable to see the height of the remote page embedded in the iFrame. This is a security restriction in browsers, as a security precaution. The assumption is that the page in the iFrame is in a different domain.
To pass the height of the content in the visible iFrame up to the parent web-page, the LoadHiddenIframe script in the remote page embeds it's height in the url of the hidden iFrame. Then, the parent page is able to grab the height from the src of the hidden iFrame, and then resize the visible iFrame. Add the following attributes to your iFrame tagName the iFrame, so the javascript functions can find it: name="surveyFrame" id="surveyFrame"
height="600"
<iframe src="http://laptopacademy.signup.sgizmo.com/" name="surveyFrame" id="surveyFrame" style="overflow: hidden" allowtransparency="true" frameborder="0" height="600" scrolling="no" width="100%" align="center"></iframe> Upload Files To Your Web HostDownload the following file, called “autofit.zip”, and unzip it. Then upload the files to the same directory as the location of your webpage which contains your survey iFrame. Advanced coders can put them into a “scripts” directory, or other standard folder. Script Your Remote PageAdd the following code to the bottom of the remote page's HTML, right before the closing </body> tag.
You MUST change the urls to point to the files you just uploaded to your server. SurveyGizmo users should go to the SurveyGizmo.com/Edit Survey/”Look and Feel” page, scroll down to “This Theme’s HTML Template”, scroll to the bottom of the html template, and paste the code at the bottom. Don’t forget to click the ’save’ button. Done!Give it a try. Load up your web-page in your browser, and then navigate through each remote page in the iFrame. The iFrame height should shrink and expand to accommodate the remote page. Notes
Alternative MethodsThe method above is the simplest way I have found to achieve cross-domain scripting. Some clever programmer might see a way to implement this method without adding any code to the remote web-page. For example, maybe you could load the remote page into a variable, and then inject the extra tags into the variable. Maybe some solution involving the DOM? One issue, in that case, may be maintaining state, so that when the client browser submits the remote page, the remote server receives the submitted page correctly. There are limitations to putting data into the url of the hidden iFrame, as described in the Tagneto reference, below. There is only so much information you can put into a url-- about 1K of data.This is fine for our simple height example, but if we want to do anything more data-intensive, we need a more robust method. Perhaps there are other ways the remote page can put the height information into the web page loaded into the hidden iFrame, besides it's url. Keep in mind, it's the remote page, which is in a different domain, which must be able to place it's height info. A control of some kind in the hidden page? There are a couple of other, more complicated, top-heavy, or esoteric ways to do cross-domain scripting, which may require the use of third-party code-libraries. Here are some resources:
If you come up with an alternate method, please post it here! References
(Page 1 of 1, totaling 2 entries)
|
RegisterSoCal Code Camp SessionOur SponsorsCollaborateSyndicate This BlogBlog Administration |
