HTA Javascript - 为什么对象超出 onbeforeunload 的范围?

发布于 2024-12-26 12:24:24 字数 1664 浏览 0 评论 0原文

这有点奇怪。我一直在尝试从父对象调用子对象中的函数,但它似乎超出了 onbeforeunload 函数的范围。这些函数调用在 onbeforeunload 之外工作,因此只有在 onbeforeunload 中调用时才会失败。我可以通过使子对象成为全局对象来修复它,但我试图避免这种情况。有人知道为什么我的子对象在 onbeforeunload 中调用时超出范围吗?请注意,我在 Windows onload 事件中调用了相同的函数,并且它工作正常。这是代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<title>Broken HTA</title>    
<HTA:APPLICATION 
 ID="Broken HTA"
 APPLICATIONNAME="Broken HTA"
 BORDERSTYLE = "flat"
 CAPTION="Yes"
 CONTEXTMENU = "no"
 INNERBORDER = "no"
 MAXIMIZEBUTTON = "no"
 MINIMIZEBUTTON = "yes"
 NAVIGABLE = "No"
 SCROLL = "auto"
 SCROLL FLAT = "Yes"
 SELECTION="no"
 SYSMENU="yes"
 SHOWINTASKBAR="yes"
 SINGLEINSTANCE="yes"
 VERSION = "1.0"
 BORDER="thick"
 >



<script language="javascript" type="text/javascript">

var myparent;

function windowLaunch()
{
    myparent = new parent();
    myparent.getchildvalue();
    window.onbeforeunload = myparent.getchildvalue;
    window.resizeTo(500, 610);
}

function parent()
{
    this.mychild = new childobject("myinput");
    this.getchildvalue = function()
    {
        var tmpval = this.mychild.returnvalue();
    };

}

function childobject(thename)
{
    this.myprop = thename;
    this.returnvalue = function()
    {
        return (document.getElementById(this.myprop).value);
    };
}

</script>
</head>

<body id="thebody" onload="windowLaunch();">
<div id="outerdiv">
        <span title="This Input Box">My Input:</span><br />
        <input id="myinput" style="width: 290px"/>
</div>
</body>

</html>

This is a bit of a strange one. I have been trying to call a function in a child object from the parent object but it seems to be going out of scope in onbeforeunload function. These function calls work outside of a onbeforeunload, so it only fails when called in onbeforeunload. I can fix it by making the child object a global but I was trying to avoid that. Anyone know why my childobject is out of scope when called in onbeforeunload? Notice I call that same function in windows onload event and it works fine. Here is the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<title>Broken HTA</title>    
<HTA:APPLICATION 
 ID="Broken HTA"
 APPLICATIONNAME="Broken HTA"
 BORDERSTYLE = "flat"
 CAPTION="Yes"
 CONTEXTMENU = "no"
 INNERBORDER = "no"
 MAXIMIZEBUTTON = "no"
 MINIMIZEBUTTON = "yes"
 NAVIGABLE = "No"
 SCROLL = "auto"
 SCROLL FLAT = "Yes"
 SELECTION="no"
 SYSMENU="yes"
 SHOWINTASKBAR="yes"
 SINGLEINSTANCE="yes"
 VERSION = "1.0"
 BORDER="thick"
 >



<script language="javascript" type="text/javascript">

var myparent;

function windowLaunch()
{
    myparent = new parent();
    myparent.getchildvalue();
    window.onbeforeunload = myparent.getchildvalue;
    window.resizeTo(500, 610);
}

function parent()
{
    this.mychild = new childobject("myinput");
    this.getchildvalue = function()
    {
        var tmpval = this.mychild.returnvalue();
    };

}

function childobject(thename)
{
    this.myprop = thename;
    this.returnvalue = function()
    {
        return (document.getElementById(this.myprop).value);
    };
}

</script>
</head>

<body id="thebody" onload="windowLaunch();">
<div id="outerdiv">
        <span title="This Input Box">My Input:</span><br />
        <input id="myinput" style="width: 290px"/>
</div>
</body>

</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2025-01-02 12:24:24

this 基于函数的调用方式。调用 myparent.getchildvalue() 没问题,但一旦将 myparent.getchildvalue 指定为处理程序,它就会被调用脱离上下文 。您可以简单地演示这一点:

var obj = { val: 42, fn: function(){ alert(this.val) } };
    ref = obj.fn;

alert(obj.fn === ref); // true, so expect the following to do the same thing...

obj.fn(); // 42, so far so good
ref(); // undefined. uh oh...

来解决这个问题:

...
window.onbeforeunload = function(){ myparent.getchildvalue(); };
...

您可以通过包装:或绑定

...
window.onbeforeunload = myparent.getchildvalue.bind(myparent);
...

this is based on how a function is called. Calling myparent.getchildvalue() is fine, but as soon as you assign myparent.getchildvalue as a handler, it will be called out of context. You can demonstrate this simply:

var obj = { val: 42, fn: function(){ alert(this.val) } };
    ref = obj.fn;

alert(obj.fn === ref); // true, so expect the following to do the same thing...

obj.fn(); // 42, so far so good
ref(); // undefined. uh oh...

You can get around this by wrapping:

...
window.onbeforeunload = function(){ myparent.getchildvalue(); };
...

or binding:

...
window.onbeforeunload = myparent.getchildvalue.bind(myparent);
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文