Refresh div on parent page from iframe using jquery

不羁岁月 提交于 2019-12-23 15:44:17

问题


I'm using fancybox's modal box with an iFrame. On form success from the iFrame I'm using the code below:

<script type="text/javascript">
window.setTimeout('window.top.location.href = "/page.asp"; ',999);
</script>

<script language="javascript">
<!--
setTimeout("parent.$.fancybox.close();",1000)
//-->
</script>

This closes fancybox's modal box and refreshes the parent page. I want to target a div to refresh on the parent page from the iFrame using Jquery.

I want the target DIV on the parent page with the id="target" to refresh, NOT the entire page. How do I go about it using JQuery?

TIA

Code Example below

Parent Page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
<div id="target">
content content
</div>
</body>
</html>

iFrame Page Upon Form Validation

<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>

<script type="text/javascript">
window.setTimeout('window.top.location.href = "/parent_page.asp"; ',999);
</script>

<script language="javascript">
<!--
setTimeout("parent.$.fancybox.close();",1000)
//-->
</script>

How do I make a function on the parent page to refresh the div id="target" and call it from the iFrame. Right now, the entire parent page refreshes.


回答1:


Pass the second parameter to jQuery, with the desired scope

Replace window.setTimeout('window.top.location.href = "/page.asp"; ',999); with this:

window.setTimeOut(function(){
    $("#target", window.parent).load("/page.asp #target");
},999);



回答2:


You can achieve this by calling a javascript function in parent page from the page contained in iframe.

window.parent.myFunctionInParent('my param');

You can learn the technique step by step in How to refresh parent page partially from page within iframe.

Hope this helps.



来源:https://stackoverflow.com/questions/6155714/refresh-div-on-parent-page-from-iframe-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!