68

Within my scenario, I have a button within an iframe section of my page that performs some database processing.

What I need is a means of performing a page refresh of the main page, when this button within the iframe is pressed.

I am seeking some JavaScript code that I can trigger within the iframe, that will reload the main window holding the iframe.

halfer
  • 19,824
  • 17
  • 99
  • 186
tonyf
  • 34,479
  • 49
  • 157
  • 246

8 Answers8

128
window.top.location.reload();
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
21

If the parent's and child iframe domains will be different you will get cross-window security error, in that case you can try to use following:

window.parent.location = document.referrer;
Sid
  • 4,302
  • 3
  • 26
  • 27
  • 2
    You are right. When using `window.top.location.reload();` code for reloading parent page within iframe from different origin this gives security error: `Blocked a frame with origin "iframe-url-here" from accessing a cross-origin frame.` But your sample works perfectly! Thank you for your answer! – Farid Imranov Feb 07 '19 at 13:39
  • `Failed to set the 'href' property on 'Location': The current window does not have permission to navigate the target frame to ''` – cdosborn Sep 27 '19 at 03:58
10
window.parent.location.reload();
Jeff Walden
  • 7,008
  • 2
  • 38
  • 55
  • 1
    Reloads frame parent rather than "Main Page", only traverses one level without additional .parent specifications, .top as Matthew suggested reloads the "Main Page" regardless of level count – Divvy Jun 15 '18 at 06:07
  • The original question description doesn't suggest there's any more than one level of frame -- nor that _if_ there is more than one level, it's desired that the topmost level of all those levels be the one reloaded. And if your main page happened to be loaded into a frame in some _other_ site, my approach _would_ reload that main page -- while reloading `window.top` would not. – Jeff Walden Jun 19 '18 at 03:36
4

We can easily achieve the facing goal by set target="_parent" to the Links which inside the iframe.

Just like the following demo shows:

<!--ParentPage.htm-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
Parent Page
<iframe src="FramePage.htm"></iframe>
</body>
</html>


<!--FramePage.htm-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<a href="http://www.g.cn" target="_parent">hello</a>
</body>
</html>
Prabhu Nandan Kumar
  • 1,205
  • 12
  • 22
1

define allFrame variable on your top frame:

var allFrame=1

and on all your frames check this function

if(top.allFrame === undefined)
{
    window.parent.location = "your website top frame";
}
0
window.opener.top.location.reload();
Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51
Serdari
  • 41
  • 3
0

If you code Page with aspx C# you can view code:

ClientScript.RegisterStartupScript(this.GetType(), "LoadParent", "<script language=javascript>window.parent.location.reload();</script>");
Zoe
  • 27,060
  • 21
  • 118
  • 148
0

This answer solved my problem. I just needed to click a link that is inside iframe so that after clicking that link the parent page in which the iframe is actually running could change in the browser.

We can easily achieve the facing goal by set target="_parent" to the Links which inside the iframe.

Just like the following demo shows:

<!--ParentPage.htm-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
Parent Page
<iframe src="FramePage.htm"></iframe>
</body>
</html>


<!--FramePage.htm-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<a href="http://www.g.cn" target="_parent">hello</a>
</body>
</html>