Go Back   SitePoint Forums > Forum Index > Program Your Site > JavaScript
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 1, 2009, 10:38   #1
=IceBurn=
SitePoint Enthusiast
 
Join Date: Jul 2008
Location: PT
Posts: 47
Unhappy Cannot Delete The iframe (iframeId is not defined)

Hello!

I'm stuck in an error while trying to destroy an iframe, and I need some help.

Here is what I have:

JavaScript Code:
<script type="text/javascript">
  function fileUpload(form, action_url, div_id) { 
    // Create the iframe... 
    var iframe = document.createElement("iframe")
    iframe.setAttribute("id","upload_iframe")
    iframe.setAttribute("name","upload_iframe")
    iframe.setAttribute("width","0")
    iframe.setAttribute("height","0")
    iframe.setAttribute("border","0")
    iframe.setAttribute("style","width: 0; height: 0; border: none;")
     
    // Add to document... 
    form.parentNode.appendChild(iframe)
    window.frames['upload_iframe'].name="upload_iframe"
   
    var iframeId = document.getElementById("upload_iframe")
     
    // Add event... 
    var eventHandler = function()  { 
     
    if (iframeId.detachEvent) 
    iframeId.detachEvent("onload", eventHandler)
    else 
    iframeId.removeEventListener("load", eventHandler, false)
     
    // Message from server... 
    if (iframeId.contentDocument) { 
    content = iframeId.contentDocument.body.innerHTML
    } else if (iframeId.contentWindow) { 
    content = iframeId.contentWindow.document.body.innerHTML
    } else if (iframeId.document) { 
    content = iframeId.document.body.innerHTML
    } 
     
    document.getElementById(div_id).innerHTML = content; 
     
    // Del the iframe... 
    setTimeout('iframeId.parentNode.removeChild(iframeId)', 250)
    } 
     
    if (iframeId.addEventListener) 
    iframeId.addEventListener("load", eventHandler, true)
    if (iframeId.attachEvent) 
    iframeId.attachEvent("onload", eventHandler)
   
    // Set properties of form... 
    form.setAttribute("target","upload_iframe")
    form.setAttribute("action", action_url)
    form.setAttribute("method","post")
     
    // Submit the form... 
    form.submit()
   
    document.getElementById("hiding").style.display = 'none';   
    document.getElementById(div_id).innerHTML = '<p class="loading">Loading...</p>'
  } 
 
</script>

I always get this error:
iframeId is not defined setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);

I dont understand why.

Any help is hightly appreciated! Thank you very much.
=IceBurn= is offline   Reply With Quote
Old Nov 1, 2009, 11:23   #2
Dan Grossman
Follow Me On Twitter: @djg
gold trophysilver trophybronze trophy
SitePoint Award Recipient
 
Dan Grossman's Avatar
 
Join Date: Aug 2000
Location: Philadephia, PA
Posts: 19,732
iframeId is declared as a local var of the fileUpload function, and isn't in scope within the anonymous function you assigned to eventhandler. Either pass it in or make it a global, or use document.getElementById within the event handler to get at it there.
Dan Grossman is online now   Reply With Quote
Old Nov 1, 2009, 14:44   #3
=IceBurn=
SitePoint Enthusiast
 
Join Date: Jul 2008
Location: PT
Posts: 47
Thank you for your reply Dan!

I ended up removing the parentNode.removeChild!

I just notice that it's not needed.
=IceBurn= is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 13:04.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved