• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Javascript question...

Atheus

Diamond Member
I have created an iframe on the fly and attached an event listener like this:

// set content to load when document is ready
if (window.addEventListener) {
editframe.addEventListener("load", setContent, false);
}
else if (window.attachEvent) {
editframe.attachEvent("******", setContent);
}


And here is the listener:

function setContent() {

var frame = frames['edit_frame'];
frame.document.getElementById('container').innerHTML=content;

// set iframe height
scaleFrame('edit_frame');

}

And everything works fine. But why doesn't this work?

function setContent() {

var frame = this;

frame.document.getElementById('container').innerHTML=content;

// set iframe height
scaleFrame('edit_frame');

}

Notice the use of 'this' to reference the calling object - doing that produces the error 'frame.document has no properties'. I know the iframe is being referenced correctly by 'this' because I can do 'alert(this.id)' and get the correct response...

Thanks for any help.


/Edit: the ****** up there is 'o n l o a d' ... apparently they censor that.
 
You sure it's not a security thing that's keeping you from accessing the document? Check the JS Console / Error Console in Firefox or SeaMonkey.
 
The document I'm loading is held locally so I don't think that's a problem. Besides, the error is just 'frame.document has no properties', nothing about security.

Would it even load via the name if there was a security restriction?
 
Originally posted by: MrChad
Have you tried using a different variable name than "frame"?

Yea. It works with 'frame' in the first example there I reference the iframe by name. Problem is it doesn't work in the second example where I use 'this'.

I have also tried passing the variable from when the iframe was originally created, something like:

editframe.addEventListener("load", setContent(temp_iframe_variable), false);

But that gave me another error.
 
Oh. Make it:

function setContent(self) {

var frame = self;

and...

if (window.addEventListener) {
editframe.addEventListener("load", setContent(editframe), false);
}

"this" from an event listener tends to not be what you want (i.e. the window)
 
Thanks CTho, but I already tried that. Same error.

It's annoying because it _should_ work, I can't see any reason why it wouldn't, and I can't find any documentation telling me why it wouldn't.

I'm just gonna have to find another way round it.
 
I got around it by by giving each new iframe an individual name, saving the string in a global variable, and having the listener access it when the iframe content has loaded. I also made it so you can't have more than one frame loading at the same time.

I can put an example anyway though:

This works...

http://213.78.133.35/cms-test/test_2.html

but this doesn't. Check the comments on the function setContent() for an explaination of the alert.

http://213.78.133.35/cms-test/test_3.html

This is how I got it to work with multiple items.

http://213.78.133.35/cms-test/test.html
 
Back
Top