Click here to Skip to main content
15,566,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am using an iframe in a page where the iframe looks like a div on the main 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:


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


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!!
ZurdoDev 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 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

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");

Something like that.
Share this answer

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

  Print Answers RSS

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900