I need help to add the new html5 event onbeforeonload to an Iframe
element.
http://www.w3schools.com/html5/html5_ref_eventattributes.asp
I'm going to do a iframe that dynamically changes the height after
different content. Everything will be in same domain. Right now I have
the problem that the height is increased but never decreased as the
new document is loaded with at least same height as the previous
document. In other words the new documents body is filled with non
content to fill up all the height in the iframe.
As an example, first load a page with very small content, then click
on a link at that page which give some content which requires a big
height (like height 2000px). The iframe should now increase the height
to match the new content. Now again click on a link back to the small
content. This small content will now get a height of 2000px (if your
longer content had 2000px).
My plans was to add a n onBeforeOnLoad event and resize the iframe
back to zero to be able to add new correct height or maybe someone has
another better solution?
I don't know if this could be added to
jsfiddle.net?
My iframeLoading.js class looks like this
var IFrameContentUpdate = new Class({
Implements: Options,
options: {
},
/*
*element = the element to load the iframe in
*src = the address to the source to load
*id = the id of the iframe object
*options for future changes
*/
initialize: function(element, src, id, options){
this.setOptions(options);
this.id = id;
this.iframeElement = new IFrame({
src: src,
styles: {
width: '100%',
border: '0px',
'y-overflow': 'hidden'
},
id: id,
events: {
load: function(){
this.setIframeHeight();
}.bind(this),
}
}).inject(element);
},
setIframeHeight: function() {
var innerDoc = (this.iframeElement.contentDocument) ?
this.iframeElement.contentDocument :
this.iframeElement.contentWindow.document;
var h = innerDoc.body.scrollHeight;
this.iframeElement.setStyle("height", h);
}
});
HTML file looks like this
<html>
<head>
<title>Iframe Content</title>
<script src="../../scripts/mootools-1.4.1-core-nc.js" type="text/
javaScript"></script>
<script src="../../scripts/iframeLoading.js" type="text/javaScript"></
script>
<script type="text/javascript">
window.addEvent('domready', function() {
var iframe = new IFrameContentUpdate($('iframeContent'),
'some_file_to_load_in_iframe.html', 'iframeElementId');
});
</script>
</head>
<body>
<div id="iframeContent" Style="height:100%;">
</div>
</body>
</html>