Jump to content

How To Resize Only 1 Iframe Height Based On Id


kalster

Recommended Posts

Hi. I have many iframes on a page. This script dynamically resizes the height of each iframes content. I would like one iframe to not dynamically resize its iframe content height.

 

i have tried adding the id code but it is not working. eg, document.getElementById("iframe").style.height=height+"px"

 

<script type='text/javascript'>
$(function(){
var iFrames = $('iframe');
function iResize() {

for (var i = 0, j = iFrames.length; i < j; i++) {
iFrames[i].style.height = iFrames[i].contentwindow.document.body.offsetHeight + 'px';}
}

if ($.browser.safari || $.browser.opera) {

iFrames.load(function(){
setTimeout(iResize, 0);
});

for (var i = 0, j = iFrames.length; i < j; i++) {
var iSource = iFrames[i].src;
iFrames[i].src = '';
iFrames[i].src = iSource;
}

} else {
iFrames.load(function() {
this.style.height = this.contentwindow.document.body.offsetHeight + 'px';
});
}

});
</script>

Link to comment
Share on other sites

the $("#iframe") without the .css property works. the problem now is that the code will only resize an iframe with an id of "iframe". i have many iframes on the page and i only want one iframe to not resize. all other iframes have an id of "iframe" now to dynamically resize those iframes. is it possible that i could have a number beside the iframe id, eg, something like $("#iframe#")

Link to comment
Share on other sites

Using an ID more than one time is invalid code, so you should fix this. At the least, you should change the ID to a class, so that all iframes have a class of .iframe (classes can be reused). Then you can give each iframe a custom ID.

Link to comment
Share on other sites

I'm saying that since you are re-using IDs in your HTML, your HTML is invalid. IDs can only be used once. Since you want to re-use them, you should be using classes instead of IDs in your HTML.

 

Then, you can give each iframe a unique ID. For example:

<iframe id="iframe1" class="some_class">
<iframe id="iframe2" class="some_class">

As you can see, I've re-used the class, and given a unique ID to each iframe. Then you can target a specific iframe in your javascript using it's ID, for example #iframe1 or #iframe2.

Link to comment
Share on other sites

well it work all the way to iframe6, then it stopped working. for example, instead of making a complicated class, i copied and pasted the iframe code as below. now when there is 6 pastes, the code does not work for some reason. it seems that i cannot have 6 iframe id at the same time on one page. below is my code. is jquery limited to how many call can be made on a single page?

 

<script type='text/javascript'>
$(function(){
var iFrames = $("#iframe1");
function iResize() {

for (var i = 0, j = iFrames.length; i < j; i++) {
iFrames[i].style.height = iFrames[i].contentwindow.document.body.offsetHeight + 'px';}
}

if ($.browser.safari || $.browser.opera) {

iFrames.load(function(){
setTimeout(iResize, 0);
});

for (var i = 0, j = iFrames.length; i < j; i++) {
var iSource = iFrames[i].src;
iFrames[i].src = '';
iFrames[i].src = iSource;
}

} else {
iFrames.load(function() {
this.style.height = this.contentwindow.document.body.offsetHeight + 'px';
});
}

});
</script>
<script type='text/javascript'>
$(function(){
var iFrames = $("#iframe2");
function iResize() {

for (var i = 0, j = iFrames.length; i < j; i++) {
iFrames[i].style.height = iFrames[i].contentwindow.document.body.offsetHeight + 'px';}
}

if ($.browser.safari || $.browser.opera) {

iFrames.load(function(){
setTimeout(iResize, 0);
});

for (var i = 0, j = iFrames.length; i < j; i++) {
var iSource = iFrames[i].src;
iFrames[i].src = '';
iFrames[i].src = iSource;
}

} else {
iFrames.load(function() {
this.style.height = this.contentwindow.document.body.offsetHeight + 'px';
});
}

});
</script>

Edited by kalster
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.