将onload函数添加到打开的窗口 [英] Add onload function to an opening window

查看:67
本文介绍了将onload函数添加到打开的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在打开的窗口中添加 onload 功能? (目标是自我,而不是 window.open ,只是常规链接)

How to add an onload function to an opened window? (target is "self", not window.open, just a regular link)

是否可以使用 onbeforeunload 还是什么?或者将 onclick 添加到普通链接?

Is it possible with onbeforeunload or something? Or add onclick to the "normal link"?

我的意思是这样的:

<a href="page2.htm" onclick="theWindowIHaveOpened.onload = function(){alert('loaded')}">...


推荐答案

在链接和外部使用onclick处理程序大块的脚本,你可以这样做:

With an onclick handler on the link and an external chunk of script, you could do something like:

<script type="text/javascript">
function loadHandler()
{
    // do stuff
}

function clickHandler(elem)
{
    h = window.open();
    h.onload = loadHandler;
    h.location.href = elem.getAttribute('href');
    return false;
}
</script>

<a href="page2.htm" onclick="return clickHandler(this);">Linky</a>

当然,你可能需要做一些工作才能让它跨浏览器工作。

Though of course you may need to do some work with that to get it working cross-browser.

编辑添加了返回语句,以便正常导航< a> 标记被禁止。

Edit Added the return statements so that normal navigation of the <a> tag is suppressed.

这篇关于将onload函数添加到打开的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆