javascriptiframeprototypejsonpaste

Why 'onpaste' event does not work in Iframe with designmode="on"?


In FireFox, I use this and it works properly,

Event.observe(iFramWin,"paste",tableAlignmentFix);

where iFramWin=$("id").contentWindow;

and in IE,

Event.observe(iFramDoc,"paste",tableAlignmentFix);

where iFramDoc =$("id").contentWindow.document;


Solution

  • In MSIE there is no onpaste-event applied to document, observe the onpaste of document.body instead.

    Example should work in both browsers(also webkit ) :

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
    <script>
    function tableAlignmentFix()
    {
      alert("tableAlignmentFix:you've pasted something");
    }
    
    function init(o)
    { 
      var doc=o.contentWindow.document;
      if(doc.getElementsByTagName('body').length)
      {
        Event.observe(doc.body,"paste",tableAlignmentFix);
        doc.designMode='on';
      }  
    }
    
    </script>
    <iframe onload="init(this);" src="about:blank" width="200" height"200"></iframe>