dynamic iframes...

F

Frances

I need to replace iframes acc. to what option user chooses in a sel
obj.. but figured have to load a blank iframe when pg loads so I can
replace it.. (iframe gets put in a pre-existing div..) this is
approach.. I'm having some problems and would appreciate some help..
thank you very much...

var selItem;
var ifrCurr;
var div = document.getElementById("divPricing");
// this var not being read inside functions..
// (even if I take out 'var' from declaration..)

function currIF() { // to load a blank iframe..
ifrCurr = document.createElement("iframe");
ifrCurr.src = 'iframes/blank.html';
//div.appendChild(ifrCurr); // ****** var not being read...

document.getElementById("divPricing").appendChild(ifrCurr);
// errror on this line
// is null or not an obj... (??)
// no problem w/this line in below function..
}
window.onload=currIF();


function pricing() { // load iframes dynamically..
selItem = document.getElementById("product").value;
var ifr = document.createElement("iframe");
ifr.src = 'iframes/' + selItem + '.html';

document.getElementById("divPricing").appendChild(ifr);
// works fine here....

// div.appendChild(ifr); // ******* var not being read...

// document.getElementById("divPricing").replaceChild(ifr,ifrCurr);
// can't use this line yet b/c problems w/function above....
}

thank you very much........ Frances
 
F

Frances

Frances said:
I need to replace iframes acc. to what option user chooses in a sel
obj.. but figured have to load a blank iframe when pg loads so I can
replace it.. (iframe gets put in a pre-existing div..) this is
approach.. I'm having some problems and would appreciate some help..
thank you very much...

var selItem;
var ifrCurr;
var div = document.getElementById("divPricing");
// this var not being read inside functions..
// (even if I take out 'var' from declaration..)

function currIF() { // to load a blank iframe..
ifrCurr = document.createElement("iframe");
ifrCurr.src = 'iframes/blank.html';
//div.appendChild(ifrCurr); // ****** var not being read...

document.getElementById("divPricing").appendChild(ifrCurr);
// errror on this line
// is null or not an obj... (??)
// no problem w/this line in below function..
}
window.onload=currIF();


function pricing() { // load iframes dynamically..
selItem = document.getElementById("product").value;
var ifr = document.createElement("iframe");
ifr.src = 'iframes/' + selItem + '.html';

document.getElementById("divPricing").appendChild(ifr);
// works fine here....

// div.appendChild(ifr); // ******* var not being read...

// document.getElementById("divPricing").replaceChild(ifr,ifrCurr);
// can't use this line yet b/c problems w/function above....
}

thank you very much........ Frances


this will not work, as users could change iframe even after they have
already chosen one.. I need to detect what iframe is loaded (i.e., what
FILE is loaded in iframe..) in order to replace it.. again thank you
very much...
 
W

web.dev

Frances said:
I need to replace iframes acc. to what option user chooses in a sel
obj.. but figured have to load a blank iframe when pg loads so I can
replace it.. (iframe gets put in a pre-existing div..) this is
approach.. I'm having some problems and would appreciate some help..
thank you very much...

var selItem;

Do you still need selItem to be a global variable? It seems you're
only using it in your pricing() function.
var ifrCurr;

var ifrCurr = null;
var div = document.getElementById("divPricing");
// this var not being read inside functions..
// (even if I take out 'var' from declaration..)

function currIF() { // to load a blank iframe..
ifrCurr = document.createElement("iframe");
ifrCurr.src = 'iframes/blank.html';
//div.appendChild(ifrCurr); // ****** var not being read...

document.getElementById("divPricing").appendChild(ifrCurr);
// errror on this line
// is null or not an obj... (??)
// no problem w/this line in below function..
}
window.onload=currIF();

I believe you don't need to load a blank iframe. So you can remove
that line and the above function.
function pricing() { // load iframes dynamically..
selItem = document.getElementById("product").value;
var ifr = document.createElement("iframe");
ifr.src = 'iframes/' + selItem + '.html';

document.getElementById("divPricing").appendChild(ifr);
// works fine here....

// div.appendChild(ifr); // ******* var not being read...

// document.getElementById("divPricing").replaceChild(ifr,ifrCurr);
// can't use this line yet b/c problems w/function above....
}

thank you very much........ Frances

You should code defensively. Ensure that you actually have an object.

function replaceIFrame()
{
if(div)
{
var selItem = document.getElementById("product").value;
var newifr = document.createElement("iframe");
newifr.src = 'iframes/' + selItem + '.html';

if(ifrCurr == null)
{
ifrCurr = div.appendChild(newifr);
}
else
{
div.replaceChild(newifr, ifrCurr);
ifrCurr = newifr;
}
}
}
 
F

Frances

web.dev said:
Do you still need selItem to be a global variable? It seems you're
only using it in your pricing() function.




var ifrCurr = null;




I believe you don't need to load a blank iframe. So you can remove
that line and the above function.




You should code defensively. Ensure that you actually have an object.

function replaceIFrame()
{
if(div)
{
var selItem = document.getElementById("product").value;
var newifr = document.createElement("iframe");
newifr.src = 'iframes/' + selItem + '.html';

if(ifrCurr == null)
{
ifrCurr = div.appendChild(newifr);
}
else
{
div.replaceChild(newifr, ifrCurr);
ifrCurr = newifr;
}
}
}

thank you VERY MUCH, web.dev......:)

following yr post this is what I have now..

function pricing() {
//if (div) {
var div = document.getElementById("divPricing");
var selItem = document.getElementById("product").value;
var ifrCurr = null;
var ifrNew = document.createElement("iframe");
ifrNew.src = 'iframes/' + selItem + '.html';

if (ifrCurr == null) {
ifrCurr = div.appendChild(ifrNew);
} else {
div.replaceChild(ifrNew,ifrCurr);
ifrCurr = ifrNew;
}
// }
}

but it still puts new iframe NEXT TO current one, not on top...

(had to take out if(div) line, script did not work at all (did not load
iframe at all) if I left it in... (??) have TONS of divs on this page,
so probably don't need this.. (it detects whether or not there are any
divs on page, right?)

again many thanks.... Frances
 
W

web.dev

Frances said:
thank you VERY MUCH, web.dev......:)

following yr post this is what I have now..

Don't forget to include:

var div = document.getElementById("divPricing");
function pricing() {
//if (div) {
var div = document.getElementById("divPricing");
var selItem = document.getElementById("product").value;
var ifrCurr = null;
var ifrNew = document.createElement("iframe");
ifrNew.src = 'iframes/' + selItem + '.html';

if (ifrCurr == null) {
ifrCurr = div.appendChild(ifrNew);
} else {
div.replaceChild(ifrNew,ifrCurr);
ifrCurr = ifrNew;
}
// }
}

but it still puts new iframe NEXT TO current one, not on top...

(had to take out if(div) line, script did not work at all (did not load
iframe at all) if I left it in... (??) have TONS of divs on this page,
so probably don't need this.. (it detects whether or not there are any
divs on page, right?)

again many thanks.... Frances

The if(div) statement is there to check if an object exists.
 
K

km0ti0n

Concider this :


var Banana = document.getElementById("Orange");
/*
This has created var var called Banana.
And stored to it a reference to an element with the id="Orange"
*/

if( Banana )
{
/*
If you reach this point there's an element on the page
with an id="Orange";
*/
}
else
{
/*
If you reach here there's no element on in the DOM
with the id="Orange";
*/
}


OK so that's why webdev was used this

function replaceIFrame()
{
var div = document.getElementById("divPricing");
if(div)
{ ...


http://km0ti0n.blunted.co.uk/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top