javascript - removeEventListner not working -
javascript - removeEventListner not working -
i have recreated problem in smallest amount of code think of lol
basically, have iframe uses addeventlistener using info user has posted though form. works well, until user posts again, , 2 eventlisteners present. have tried using removeeventlistener not working in case.
here have done recreate problem:
testbuffer.php<html> <head> <script> function startlistener() { parent.document.removeeventlistener('mouseup',outputinput,false); parent.document.addeventlistener('mouseup',outputinput,false); } function outputinput() { console.log('<?php echo $_post['input']; ?>'); } </script> </head> <body onload='startlistener()'> </body> </html> testparent.php <iframe name="bufferiframe" id="bufferiframe"></iframe> <form name="test" method="post" action="testbuffer.php" target="bufferiframe"> <input type="text" name="input"> <input type="submit" value="go"> </form> here's happens if follow these steps:
enter 'hello' , click 'go' click anywhere on screen, 'hello' appears in console. enter 'bye' , click 'go' click anywhere on screen, 'hello' , 'bye' appears in console.i have tried function as:
function startlistener() { parent.document.addeventlistener('mouseup',outputinput,false); parent.document.removeeventlistener('mouseup',outputinput,false); parent.document.addeventlistener('mouseup',outputinput,false); } with same result
any help appreciated :d
you'll need flush of mouseup events document. here, have 2 options.
option 1: uses jquery, clean:
use
function startlistener() { $(parent.document).off('mouseup'); $(parent.document).on('mouseup', function () { // event handler stuff here }); } into testbuffer.php.
option 2: doesn't utilize jquery, dirty:
use (written me, no help whatsoever):
var el = parent.document.body, elclone = el.clonenode(true); el.parentnode.replacechild(elclone, el); in testbuffer.php, , attach events body instead of document:
parent.document.body.addeventlistener('mouseup', outputinput, false); javascript addeventlistener
Comments
Post a Comment