Skip to content Skip to sidebar Skip to footer

Adding Event Handler To Iframe To Invoke On Keyup

On the webpage I have iframe, in which I display some content. I want to run a function if any key is pressed inside iframe (iframe is editable). I can do it using ajax (with scrip

Solution 1:

I guess you meant "do it without using the script manager"?

You can do it this way (use attactEvent/addEventListener):

<html><head><scripttype="text/javascript">functionInit() {
    var _win = document.getElementById("iView").contentWindow;
    var _doc = _win.document;
    _doc.designMode = "on";
    if(_doc.addEventListener) {// if support addEventListener
        _doc.addEventListener("keyup", frameKeyUp, true)
    }
    else { //For IE
        _doc.attachEvent("onkeyup", frameKeyUp);
    }
}
functionframeKeyUp(e){
    alert(e.keyCode);
}
</script></head><bodyonload="Init()" ><iframeid="iView"style="width: 434px; height:371px"></iframe></body></html>

Or using jQuery's event handler for "keyup":

$('#iframe1').keyup(function(event){
  //... do your stuff here ..//use event.keyCode to get the asscoiated key
});

More info on keyboard event arg can be found here and here.

Post a Comment for "Adding Event Handler To Iframe To Invoke On Keyup"