Link to home
Start Free TrialLog in
Avatar of oleber
oleberFlag for Portugal

asked on

Changing the iframe content with jQuery

For having some big areas with scroll bars inside my web page, I'm willing to use an iframe. Maybe it isn't a good idea, open to suggestions in here.

I'm using the library jQuery, and I was considering to play with the iframe DOM from the parent window.

The child window(iframe) will not load an url. If is supposed to be changed (it has a really dynamic content) by the parent only.


Who do I change the iframe DOM?
What other solution do I have? (using just html and Javascript)

ASKER CERTIFIED SOLUTION
Avatar of bored321
bored321
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is the content to be shown in the scrolling area to be retrieved from external HTML files, like the iframe would?

If so, you can go with an idea like bored321 says:


// Scrolling area:
 
<div id="content1" style="overflow:auto; width:400px; height:400px;"></div>
 
// function jQuery to populate scrolling Div with external content:
 
function getContent(targetDiv,page){
         $.get(page,function(content){
             $(targetDiv).html(content);
         });
}
 
// Command to populate on page load:
 
$(document).ready(function(){
    getContent('#content1','/path/to/external.html');
});
 
// Linked Nav would be like this:
 
<a href="javascript:getContent('#content1','/path/to/external.html');return false;">Page 1</a>
<a href="javascript:getContent('#content2','/path/to/external2.html');return false;">Page 2</a>

Open in new window

I tried the following other method:

        function getContent(targetDiv, page) {
            alert('Running getContent');

            $.get(page, function (content) {
                $(targetDiv).html(content);
            });
        };

....
....


    <div id="content1" style="overflow:auto; width:50px; height:50px;"></div>
    <a href="javascript:getContent('#content1','www.yahoo.com');return false;">Page 1</a>

Open in new window


And clicking the "Page 1" anchor did not find the div and so no action or alert message.