Firefox Javascript XUL中文本区域节点的触发警报功能事件

时间:2011-09-08 09:50:59

标签: javascript events xul alert onkeypress

如果用户达到文本区域中的最大字符数,我想触发警报。 通常我的插件将用户节点值直接填充到我的插件文本框中。所以,如果用户达到var mymaxlength = 20;

,我想生成警报
var mymaxlength = 20;
    if ( nodeName == "textarea" && node.value.length >= mymaxlength ) {

        // call your popup / alert function 
                        alert('hi, you have reached 20 characters');
        }

我已经尝试了上面的代码,没有任何错误它没有工作?在我的完整代码中,一般警报正在工作,但循环内的警报不起作用? 1.它是在onKeypress中触发警报的好方法:函数(e)?要么 2.如果用户节点在fillText:function(节点)中提交了20个字符,是否可以触发警报?

请帮助我!

这是完整的代码:

 run : function () {
                //alert(content.document.cookie);
                //alert("-"+content.document.cookie+"-");
                var cookieTest = content.document.cookie
                var JSESSIONID = pdees.get_Cookie("JSESSIONID");
                if(verifyConnection){
                    if(JSESSIONID && cookieTest){
                        //var result = verifyUserIdentity(JSESSIONID);
                        var head = content.document.getElementsByTagName("head")[0];
                        var body = content.document.body;
                        //var style = content.document.getElementById("pdees-style");
                        //var allLinks = content.document.getElementsByTagName("pdees");
                        var foundLinks = 0;

                        //extract text element from body
                        if(document.getElementById && document.createTreeWalker && typeof NodeFilter!="undefined"){
                            var tw=document.createTreeWalker(body,NodeFilter.SHOW_TEXT,null,false);
                            lookForpdees(tw);
                        }
                        addJScode();
                        idpdeesbutton=0;
                    }else{
                        alert("You should connect to the Application Environment to be authentified");
                    }
                }else{
                    //var result = verifyUserIdentity(JSESSIONID);
                    var head = content.document.getElementsByTagName("head")[0];
                    var body = content.document.body;
                    //var style = content.document.getElementById("pdees-style");
                    //var allLinks = content.document.getElementsByTagName("pdees");
                    var foundLinks = 0;

                    //extract text element from body
                    if(document.getElementById && document.createTreeWalker && typeof NodeFilter!="undefined"){
                        var tw=document.createTreeWalker(body,NodeFilter.SHOW_TEXT,null,false);
                        lookForpdees(tw);
                    }
                    addJScode();
                    idpdeesbutton=0;
                }
            },

            onKeypress : function (e) {

             var mymaxlength = 20;  
                var node = e.target;
                var nodeName = node.nodeName.toLowerCase();
                //text area cache onKeyPress code
                //alert('hi1');


                if ( nodeName == "textarea" && node.value == "" && e.keyCode == 13 ) {

                  pdees.fillText(node);

                  return;
                }
      if ( nodeName == "textarea" && node.value.length >= mymaxlength ) {

                // call your popup / alert function 
                                alert('hi, you have reached 20 characters');
                }


                // this node is a WYSIWYG editor or an editable node?
                if ( ( nodeName != "html" || node.ownerDocument.designMode != "on" ) && node.contentEditable != "true" )
                  return;

                if ( node.textContent == "" && e.keyCode == 13 ) {
                  pdees.fillText(node);
                  return;
                }

                 if (!node.tacacheOnSave) {
                  pdees.fillText(node);
                }

            },
            onChange : function (e) {
                var node = e.target;
                var nodeName = node.nodeName.toLowerCase();
                //alert("onChange : "+nodeName);
                if ( nodeName != "textarea" )
                return;
                pdees.fillText(node);
            },
            onInput : function (e) {
                var node = e.target;
                var nodeName = node.nodeName.toLowerCase();
                //alert("onInput : "+nodeName);
                // Only for textarea node
                if ( node.nodeName.toLowerCase() != "textarea" )
                  return;

                if ( node.value == "" )
                  return;
                pdees.fillText(node);


            },
            fillText : function (node) {
                nodeSRC = node;

                if ( node.nodeName.toLowerCase() == "textarea" )
     { 
                    //alert('hi');
                  userContent = node.value;
                  //alert(userContent);
                }

                else if ( node.nodeName.toLowerCase() == "html" ) { 
                  userContent = node.ownerDocument.body.innerHTML;
                  //alert(userContent);}
                }
                else // element.contentEditable == true
                  userContent = node.innerHTML;
            },
            emptyNodeSRC : function (node){
                if ( node.nodeName.toLowerCase() == "textarea" ) {
                  node.value = "";
                }
                else if ( node.nodeName.toLowerCase() == "html" ) {
                  node.ownerDocument.body.innerHTML = "";
                }
                else // element.contentEditable == true
                  node.innerHTML = "";
            },

        };
    }();    

1 个答案:

答案 0 :(得分:1)

最后,我发现了我的问题,并且我已成功触发mu自定义警报: 在此函数fillText : function (node)中,我可以触发userContent的警报。所以我做了一个长度并触发了警报。

else if ( node.nodeName.toLowerCase() == "html" ) { 

             userContent = node.ownerDocument.body.innerHTML;
             //alert(userContent);
              var myTest = userContent.length;
              if(userContent.length == 30)
              { 
                    alert('Hi, there!');
              }

            }
            else // element.contentEditable == true
              userContent = node.innerHTML;
        },

注意:有关完整代码,请参阅我的问题。