I have a web page with an application running in an iframe (same domain). The iframe's height is set on load based on the iframed document's height. The problem is when the iframed document's size is changed (by expanding an accordion, menu etc). Are there any events fired when this occurs, that I can use to resize the iframe element accordingly?
I have tried to bind to the resize event on the window object in the iframe, but as the window isn't resized when the document content changes, the event doesn't fire. What I need is some kind of resize-event on the document object, but as far as I know, there is no such thing. Is there another way to detect changes in the document's height?
I'd appreciate a general solution, as I don't know the content of the iframe exactly, but I can include generic scripts in it.
There are resize events fired for the iframe window. You can listen to them (in the iframe itself) and than trigger some function in the parent window to propagate new size and update the iframe size in the parent window.
Have you tried using the scroll event and attaching it to the contentWindow of the iFrame?
Otherwise I would rather suggest you hook directly into the accordion rather than relying on catching scroll events that may or may not happen.
Because scroll will only fire once the user actually scrolled the slider, not when the accordion expands..
But you could simply call a method on the parent when the user expands the accordion passing it the new size of the accordion.
I found a possible, but not ideal solution, and it doesn't work in all browsers:
By binding to the body element's DOMSubtreeModified in the iframe, I can find the iframe element in parent or top and change it's height.
$('body').bind('DOMSubtreeModified', function(){
top.document.getElementById('appFrame').height = $(document).height();
});
Here's an overview of the browser support for this event:
http://www.quirksmode.org/dom/events/
If you are using jQuery ui then you can use event change and call function
setHeight
http://jqueryui.com/demos/accordion/
function setHeight() {
parent.document.getElementById('the-iframe-id').height = document['body'].offsetHeight;
}
Related
I have a popup div in which I have an iframe. Initially, I have set the height of iframe to some px according to the content in it.
Now, I have a click event on the iframe from which I am redirecting to another view but this view has more content to fit the height. So I want to change the height of the iframe on that event or on the load of other view but I am unable to access the iframe from within that click event of a button.
EasyXDM provides a bunch of useful functions in order to manipulate communication between the client and fetched iframes
Here is an example similar with what you want to achieve.
Keep in mind that you should also control the content into the iframe as well.
* Please note: cross-domain scenario *
I have an iframe which has no scrollbar, but much more taller than the browser window, so the vertical scrolling is done by the browser's main vertical scrollbar. However there is a disturbing feature of this: When the navigation done in scrolled down state the scroll is not reset to top in the parent.
I would like to reset this scroll to the init top state automatically when page navigation occurs in the iframe.
Thanks in advance.
If your iframe has a cross domain issue (pointing to somewhere other than your website) You can't:
error : Permission denied to access property 'document'
But if not, you can do it using this code. add this to scripts in page:
var myframe = window.frames[0].window;
myframe.onscroll = function(){if(myframe.scrollY == myframe.scrollMaxY){window.scrollTo(0,0);}};
//Thanks Diodeus!
Or you can select it by getElementById or other similars and give the reference to myframe variable. This triggers an event when you scroll down in the iframe and then scrolls the whole document up.
You can hook an onload event to the iframe. It is one of the few hooks you can get in a cross-domain scenario.
document.getElementById("some_iframe").onload = function (){ window.scrollTo(0,0); }
Is there a way of resizing an iFrame when the content within changes dimensions, for example
after a javascript effect?
In my example link I have used a Show/Hide Javascript Effect, but the resulting overflow is getting hidden because my iframe can't communicate.
My Website Example (Click Page 2 and you'll see the longer text is hidden)
Thanks
Yes it is possible. It's done by Facebook app for example.
To access the iframe from the main document:
window.frames['iframeName'].document.callAFunction()
window.frames['iframeName'].document.getElementById('foo')
From iframe to parent document:
parent.document.callAFunction()
parent.document.getElementById('foo')
Keep in mind that this only works, if both documents loaded from the same domain.
Now you can do:
Fetch an event if the iframe content changes
Submit the new size into the parent document
The parent document changes the iframes dimension
Enclose the iFrame in a div, assign width and height of iFrame to 100% and control the height/width of the div.
I have a app in Taobao.com, when the app loads, Taobao.com loads the html content from my callback url, and constructs a iframe in final page. Now I want a div in my html to move up and down with the scroll of the outer page (in Taobao.com, this div is positioned in a iframe). How to response the scroll event of outer parent page in a iframe? Is there any other solutions to implement my effect?
I've tried
$(parent).scroll(function(){})
and
$(parent.document).scroll(function(){})
both not working.
I think you are looking for
$(window.parent.document).scroll(function() {});
update
here's an example on jsfiddle with the mousemove event: http://fiddle.jshell.net/DuBPL/1/ (no scroll because of the interface of jsfiddle, but you'll see it does work)
Note that this won't (ever) work if you have an iFrame with for example the src domain1.com and the parent on domain2.com.
I have a page with an iframe. Inside the iframe is code (that I can't change) that sets location.hash to the id of an element in the iframe window.
This has the unwanted effect of scrolling my outermost browser window so that the top of the window touches the top of the iframe. This is quite annoying as I have a toolbar above the iframe that is vital to my app.
Is there any way of preventing the setting of location.hash affecting the scroll position of the main window?
Will preventDefault help me out here?
EDIT: It looks like this isn't really possible in the way I described it.
If preventDefault doesn't do what you want it to, you could do is dynamically move the location of the <a name='iframehash'> so that it's always at the top of the screen. That way, when the call to move to whatever hash it's going to gets called, nothing will actually move.
But if preventDefault() works, it's a much better solution.
You can use:
event.preventDefault()
This will stop the default action of the element it is applied to.
https://developer.mozilla.org/en/DOM/event.preventDefault