Alert and if changes behaviour dom copying

Posted by lowkey on Stack Overflow See other posts from Stack Overflow or by lowkey
Published on 2010-03-14T22:35:16Z Indexed on 2010/03/14 22:45 UTC
Read the original article Hit count: 206

Filed under:
|
|
|

Hi guys! Tried to make a little old school ajax (iframe-javascript) script. A bit of mootools is used for DOM navigation

Description:

HTML:

1 iframe called "buffer"

1 div called "display"

JAVASCRIPT: (short: copy iframe content into a div on the page)

1) onLoad on iframe triggers handler(), a counter makes sure it only run once (this is because otherwise firefox will go into feedback loop. What i think happens: iframe on load > handler() > copyBuffer > change src of iframe , and firefox takes that as an onload again)

2) copybuffer() is called, it sets src of iframe then copies iframe content into div, then erases iframe content

THE CODE:


count=0;
function handler(){

if (count==0){
copyBuffer();
count++;
}

}

function copyBuffer(){


$('buffer').set('src','http://www.somelink.com/');

if (window.frames['buffer'] && $('display') ) { 

$('display').innerHTML = window.frames['buffer'].document.body.innerHTML;
window.frames['buffer'].document.body.innerHTML="";
}

}

problems:

QUESTION 1) nothing happens, the content is not loaded into the div. But if i either:

A) remove the counter and let the script run in a feedback loop: the content is suddenly copied into the div, but off course there is a feedback loop going on, you can see it keeps loading in the status bar.

OR

B) insert an alert in the copyBuffer function. The content is copied, without the feedback loop.

why does this happen?

QUESTION 2) The If wrapped around the copying code i got from a source on the internet. I am not sure what it does? If i remove it the code does not work in: Safari and Chrome.

Many thanks in advance!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about copy