Как вызвать функцию родительского окна из дочернего окна jquery?
Мне просто нужно вызвать функцию в родительском окне, в то время как пользователь фокусируется на дочернем окне.
У меня этот код в моем родительском окне,
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="Javascript" type="text/javascript">
function CallParent()
{
alert(" Parent window Alert");
}
</script>
<body>
<a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("child.html","Ratting","width=550,height=170,0,status=0,");>Click here to open the child window</a>
</body>
</html>
и ниже код находится в моем дочернем окне,
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="Javascript" type="text/javascript">
jQuery(document).ready(function(){
window.opener.CallParent();
});
</script>
</head>
<body>
<h2>This is Child window</h2>
</body>
</html>
Итак, в этом случае я предположил, что CallParent()
будет запущен сразу после открытия дочернего окна. но, похоже, он не работает.
может ли кто-нибудь дать мне какие-либо намеки на то, чтобы этот script работал, или лучший способ сделать это.
Ответы
Ответ 1
Использовать этот
window.parent.CallParent();
вместо
window.opener.CallParent();
window.parent
содержит ссылку на родителя текущего окна или подкадра.
Если у окна нет родителя, его parent
свойство является ссылкой на себя.
Когда окно загружается в <iframe>
, <object>
или <frame>
, его родителем является окно с элементом, внедряющим окно.
Ссылка: https://developer.mozilla.org/en-US/docs/Web/API/Window/parent
Ответ 2
Попробуйте что-нибудь вроде:
parent.html
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="Javascript" type="text/javascript">
window.CallParent = function() {
alert(" Parent window Alert");
}
</script>
<body>
<a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("child.html","Ratting","width=550,height=170,0,status=0,");>Click here to open the child window</a>
</body>
</html>
child.html
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="Javascript" type="text/javascript">
jQuery(document).ready(function(){
opener.CallParent();
});
</script>
</head>
<body>
<h2>This is Child window</h2>
</body>
</html>
Вы не должны делать это на родительском, иначе openener.CallParent() не будет работать, делая window.CallParent делает CallParent доступным как область окна:
function CallParent() {
alert(" Parent window Alert");
}
И тогда вы можете просто вызвать opener.CallParent();
not window.opener.CallParent();
.
Ответ 3
Я использовал следующий код parent.html
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="Javascript" type="text/javascript">
function CallParent()
{
alert(" Parent window Alert");
}
</script>
<body>
<a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("child.html","Ratting","width=550,height=170,0,status=0,");>Click here to open the child window</a>
</body>
</html>
child.html
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="Javascript" type="text/javascript">
window.opener.CallParent();
</script>
</head>
<body>
<h2>This is Child window</h2>
</body>
</html>