Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi, I am using an iframe in a page where the iframe looks like a div on the main asp.net page. In the Iframe page i have got a close button and when i click on close button i am calling a javascript function in iframe page which is having the code as:
 
window.location.href = "Pagename.aspx";
 
this line closes the Iframe but the main page which contains Iframe refreshes after closing the iframe so.... I tried in many ways to close the iframe without refres like
 
window.close();
 
but it is not working. The main problem to close the iframe is that the close btn is in the iframe page it self so if i write the code to close iframe in iframe page that does not work.....
 

So, can u please help me solve this issue .. that is closing of iframe without refreshing main page that contains iframe. Thanking you in advance!!
Posted 17-Oct-12 5:15am
Comments
ryanb31 at 17-Oct-12 10:30am
   
What do you mean by closing the iframe. The iframe is a window in the window so if you did actually close it what would be left in its place?
TheCoolCoder at 18-Oct-12 0:27am
   
window.location.href = "Pagename.aspx"; where do u call this script?? inside iframe window or on the main page?? anyways this wont close your iframe, it will only load the current browser window with 'Pagename.aspx', also if your requirement is to cause the iframe to disappear on a button click, just hide it and if you dont want any server side functioning for your close button, try replacing your close button with a html button <input id="btnClose" type="button" value="button" /> so that it wont postback by default.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You'll need to destroy the iframe component. An I frame is not a window. So window.close won't do anything. You can however call a function in the top level window. As long as the content of you iframe is in the same domain.
 
So instead of window.close() you do window.parent.closeIframe();
 
The in the parent window you'll need a function closeIframe();
 
function closeIframe() {
   var ifram=document.getElementById("iframe-id");
   iframe.parent.removeChild(iframe);
}
 
Something like that.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100