Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript event not being set correctly in closure when called cross frame
    primarykey
    data
    text
    <p>I have the following code in the top frame of a two frame page:</p> <pre><code> function setKeyHook() { logMessage("setKeyHook()"); top.frames.BOTTOM.document.onkeydown = top.frames.TOP.document.onkeydown = function( evt ) { return function(){ top.frames.TOP.handleKeypress(evt); }; }( window.event ); } onload = setKeyHook; </code></pre> <p>This works on the original document load, but when I call this function from another frame (usually when only one frame reloads), the hook is set, but when the onkeydown function fires, it dose not recieve the appropriate arguments, instead evt == null.</p> <p>Full Code follows:</p> <p><strong>KeyFrameTest.asp</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;KeyFrameTest&lt;/title&gt; &lt;/head&gt; &lt;frameset Rows="80%,20%"&gt; &lt;frame id="TOP" name="TOP" src="KeyFrameTestTop.asp"&gt; &lt;frame id="BOTTOM" name="BOTTOM" src="KeyFrameTestBottom.asp"&gt; &lt;/frameset&gt; &lt;/html&gt; </code></pre> <p><strong>KeyFrameTestTop.asp</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;script type="Text/Javascript"&gt; String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); } NumElements = 5; //Alt Vals ControlCode = 1; ShiftCode = 2; ControlShiftCode = 3; //Key Vals keyR = 82; keyJ = 74; keyI = 73; keyT = 84; keyEnter = 13; //Array Indexs AltIndex = 0; KeyIndex = 1; FuncIndex = 2; KeyFuncMap = new Array(NumElements); for (i = 0; i &lt; KeyFuncMap.length; ++i) { //Three elements, control or shift, key, function KeyFuncMap[i] = new Array(3); } KeyFuncMap[0][AltIndex] = ControlCode; KeyFuncMap[0][KeyIndex] = keyR; KeyFuncMap[0][FuncIndex] = "parent.TOP.logMessage(\"Ctrl + R\")"; KeyFuncMap[1][AltIndex] = ControlCode; KeyFuncMap[1][KeyIndex] = keyJ; KeyFuncMap[1][FuncIndex] = "parent.TOP.logMessage(\"Ctrl + J\")"; KeyFuncMap[2][AltIndex] = ControlCode; KeyFuncMap[2][KeyIndex] = keyI; KeyFuncMap[2][FuncIndex] = "parent.TOP.logMessage(\"Ctrl + I\")"; KeyFuncMap[3][AltIndex] = ControlCode; KeyFuncMap[3][KeyIndex] = keyT; KeyFuncMap[3][FuncIndex] = "parent.TOP.logMessage(\"Ctrl + T\")"; KeyFuncMap[4][AltIndex] = ControlCode; KeyFuncMap[4][KeyIndex] = keyEnter; KeyFuncMap[4][FuncIndex] = "parent.TOP.logMessage(\"Ctrl + Enter\")"; function CompleteEvent(e) { e.cancelBubble = true; e.returnValue = false; } function logMessage(msg) { logBox = parent.TOP.document.getElementById("logBox"); if( logBox.value.trim().length &lt; 1 ) { logBox.value = msg; } else { logBox.value = logBox.value + "\r\n" + msg; } } function handleKeypress(e) { logMessage("handleKeypress(e)"); e = e || window.event ; if (e == null) { logMessage("handleKeypress(e): e == null"); return false; } controlVal = getControlVal(e); for (i = 0; i &lt; KeyFuncMap.length; i++) { if (KeyFuncMap[i][AltIndex] == controlVal &amp;&amp; KeyFuncMap[i][KeyIndex] == e.keyCode) { eval(KeyFuncMap[i][FuncIndex]); CompleteEvent(e); } } } function getControlVal(e) { if (e.ctrlKey &amp;&amp; e.shiftKey) { return 3; } else if (e.ctrlKey) { return 1; } else if (e.shiftKey) { return 2; } else return 0; } function displayEverything() { displayProps(top.frames.TOP, "top.frames.TOP", 0, 1); displayProps(top.frames.BOTTOM, "top.frames.BOTTOM", 0, 1); } function clearLog() { logBox = parent.TOP.document.getElementById("logBox"); logBox.value = ""; } function displayProps(o, name, level, maxLevel) { try { if (level &gt; maxLevel) return; for (prop in o){ logMessage(name + "." + prop + " = " + o[prop]); if (typeof(o[prop]) == "object" &amp;&amp; o[prop] != o){ displayProps(o[prop], name + "." + prop, level + 1, maxLevel); } } } catch (ex){ logMessage(ex.toString()); } } function setKeyHook() { logMessage("setKeyHook()"); top.frames.BOTTOM.document.onkeydown = top.frames.TOP.document.onkeydown = function( evt ) { return function(){ top.frames.TOP.handleKeypress(evt); }; }( window.event ); } onload = setKeyHook; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Hello&lt;/h1&gt; &lt;textarea id="LogBox" rows="20" cols="80"&gt;&lt;/textarea&gt;&lt;BR&gt; &lt;input type="Button" value="Display Properties" onClick="displayEverything();"/&gt; &lt;input type="Button" value="Clear Log" onClick="clearLog();"/&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>KeyFrameTestBottom.asp</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;Press Keys Here&lt;/p&gt; &lt;input type="Button" value="Reset Handlers" onclick="top.frames.TOP.setKeyHook();"&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>To recreate the issue, right click the bottom frame, click refresh, click "Reset Hooks", and press keys.</p> <p>Related Question: <a href="https://stackoverflow.com/questions/1044573/handle-keypress-accross-frames-in-ie">Handle keyPress Accross Frames in IE</a></p> <p>I've also read an article on <a href="http://www.jibbering.com/faq/faq_notes/closures.html" rel="nofollow noreferrer">Javascript closures</a>, but I'm not sure how it applies.</p> <p>Sorry for the narrowness of question, but I really don't know Javascript well enough to figure out the trick to this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload