Javascript on Mozilla? working on IE.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skyy
    New Member
    • May 2007
    • 109

    Javascript on Mozilla? working on IE.

    Hi.. i got the following javascript. Its working when using IE6.. but when i use mozilla 1.0.1 it does not work at all..

    <SCRIPT language="JavaS cript">
    var upload_number=2 ; function addFileInput()
    {
    if(upload_numbe r>10)
    {alert('You can only upload 10 files at one time');
    return;}
    var moreuploads=doc ument.getElemen tById("moreuplo ads");
    var d=document.crea teElement("td") ;
    d.style.fontWei ght='bold';
    var file=document.c reateElement("i nput");
    var txt1=document.c reateTextNode(' File name: ');
    var txt2=document.c reateTextNode(' Location: ');
    var name=document.c reateElement("i nput");
    file.setAttribu te("type", "file");
    file.setAttribu te("name", "datafile"+uplo ad_number);
    file.setAttribu te("size", "25");
    file.attachEven t("onchange",ch eckfilename2);
    name.setAttribu te("type", "text");
    name.setAttribu te("name", "fname"+upload_ number);
    name.setAttribu te("id", "fname"+upload_ number);
    name.setAttribu te("size", "10");
    d.appendChild(t xt1);
    d.appendChild(n ame);
    d.appendChild(t xt2);
    d.appendChild(f ile);
    moreuploads.app endChild(d);
    upload_number++ ;
    }

    Any idea???
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    the attachEvent-method is IE-specific, all other browsers use addEventListene r instead ... so you have to differ between browsers ... you may ask the window for the method:

    [CODE=javascript]
    if (typeof window.addEvent Listener != 'undefined') {
    obj.addEventLis tener(e, handler, false);
    } else if (typeof window.attachEv ent != 'undefined') {
    obj.attachEvent ('on' + e, handler);
    } else {
    // do something other obj.onclick = handler etc.
    }[/CODE]

    kind regards ...

    Comment

    • skyy
      New Member
      • May 2007
      • 109

      #3
      Originally posted by gits
      hi ...

      the attachEvent-method is IE-specific, all other browsers use addEventListene r instead ... so you have to differ between browsers ... you may ask the window for the method:

      [CODE=javascript]
      if (typeof window.addEvent Listener != 'undefined') {
      obj.addEventLis tener(e, handler, false);
      } else if (typeof window.attachEv ent != 'undefined') {
      obj.attachEvent ('on' + e, handler);
      } else {
      // do something other obj.onclick = handler etc.
      }[/CODE]

      kind regards ...
      Hi...

      that works... thanks...

      I got another problem.. the following function doesnt work on mozilla too...

      function checkfilename2( )
      {
      var p=event.srcElem ent;
      var n=p.name;
      var num=n.substr(8) ;
      var path=p.value;
      var fname=path.subs tring(path.last IndexOf("\\\/")+1);
      var filename=docume nt.getElementBy Id('fname'+num) ;
      if(fname.indexO f("\/")>-1)
      {
      filename.value = ""; alert("The '/' character is not allowed in the filename! Please specify another file name.");
      }
      else
      filename.value = fname;
      }

      Any idea?

      Comment

      • rnd me
        Recognized Expert Contributor
        • Jun 2007
        • 427

        #4
        Originally posted by skyy
        Hi...

        that works... thanks...

        I got another problem.. the following function doesnt work on mozilla too...

        function checkfilename2( )
        {
        var p=event.srcElem ent;
        var n=p.name;
        var num=n.substr(8) ;
        var path=p.value;
        var fname=path.subs tring(path.last IndexOf("\\\/")+1);
        var filename=docume nt.getElementBy Id('fname'+num) ;
        if(fname.indexO f("\/")>-1)
        {
        filename.value = ""; alert("The '/' character is not allowed in the filename! Please specify another file name.");
        }
        else
        filename.value = fname;
        }

        Any idea?
        there is no srcElement outside of ie. change the code, or define srcElement for other browsers:

        [PHP]

        Event.prototype .__defineGetter __("srcElement" , function () {
        var node = this.target;
        while (node.nodeType != 1) {node = node.parentNode ;}
        return node;
        });[/PHP]

        Comment

        • skyy
          New Member
          • May 2007
          • 109

          #5
          Originally posted by rnd me
          there is no srcElement outside of ie. change the code, or define srcElement for other browsers:

          [PHP]

          Event.prototype .__defineGetter __("srcElement" , function () {
          var node = this.target;
          while (node.nodeType != 1) {node = node.parentNode ;}
          return node;
          });[/PHP]

          Hi..

          it doesnt work for mozilla even when i change the srcElement to target.

          var p=event.target;

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by skyy
            Hi..

            it doesnt work for mozilla even when i change the srcElement to target.

            var p=event.target;
            Again, window.event is IE-only. In your function, add "event" as an argument even though you call it without arguments: [CODE=javascript]function checkfilename2( event) {[/CODE]

            Comment

            • skyy
              New Member
              • May 2007
              • 109

              #7
              Originally posted by acoder
              Again, window.event is IE-only. In your function, add "event" as an argument even though you call it without arguments: [CODE=javascript]function checkfilename2( event) {[/CODE]

              ok thanks!

              changed to..

              function checkfilename2( event)
              {
              var p=event.srcElem ent
              ......
              }

              but still can't work....

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                See the cross-browser way to do it here.

                Comment

                • skyy
                  New Member
                  • May 2007
                  • 109

                  #9
                  Hi... looking at the various recommendation. . i made some changes but it still doesnt work..

                  function checkfilename2( e)
                  {
                  var p;
                  if(!e) var e=window.event;
                  if(e.target) p=e.target;
                  else p=e.srcElement;
                  var n=p.name;
                  var num=n.substr(8) ;
                  var path=p.value;
                  var fname=path.subs tring(path.last IndexOf("\\\/")+1);
                  var filename=docume nt.getElementBy Id('fname'+num) ;
                  if(fname.indexO f("\/")>-1)
                  {
                  filename.value = ""; alert("The '/' character is not allowed in the filename! Please specify another file name.");
                  }
                  else
                  filename.value = fname;
                  }

                  Any idea?

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    hi ...

                    compare your script with the following slightly adapted (working) code:

                    [CODE=javascript]
                    <script>
                    function checkfilename2( e) {
                    var p;

                    // don't declare e again - it will hide the function argument
                    // you only need to assign it
                    if(!e) {
                    e = window.event;
                    }

                    if(e.target) {
                    p = e.target;
                    } else {
                    p = e.srcElement;
                    }

                    var n = p.name;
                    var num = n.substr(8);
                    var path = p.value;
                    var fname = path.substring( path.lastIndexO f("\\\/")+1);
                    var filename = document.getEle mentById('test' );

                    if(fname.indexO f("\/") > -1) {
                    filename.value = "";
                    alert("The '/' character is not allowed.");
                    } else {
                    filename.value = fname;
                    }
                    }
                    </script>

                    <input type="text" id="test" name="test" onclick="checkf ilename2(event) ;"/>
                    [/CODE]

                    i simplified it for me to test ... readapt it to your purpose ...

                    besides that: perhaps users find it annoying to have their input deleted ... may be they put the slash without wanting this ... and now they have to write the entire text again ... may be you only want to delete the slash or give the user a hint that he has to do that for his/herself ...

                    kind regards ...

                    Comment

                    • skyy
                      New Member
                      • May 2007
                      • 109

                      #11
                      Originally posted by gits
                      hi ...

                      compare your script with the following slightly adapted (working) code:

                      i simplified it for me to test ... readapt it to your purpose ...

                      besides that: perhaps users find it annoying to have their input deleted ... may be they put the slash without wanting this ... and now they have to write the entire text again ... may be you only want to delete the slash or give the user a hint that he has to do that for his/herself ...

                      kind regards ...
                      Hi.. thanks for trying out for me...

                      But i think the problem now is that i can pass the event handler over to the checkfilename2 function.

                      I am using the addEventListene r to attach the "onclick" event to the input

                      file.addEventLi stener("click", function() { checkfilename2( event);},false) ;

                      this seems to be not working...

                      even after i changes my code to the format of ur code.. thanks..

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5390

                        #12
                        try the following ;)

                        [CODE=javascript]file.addEventLi stener("click", function(event) { checkfilename2( event);}, false);[/CODE]

                        kind regards ...

                        Comment

                        • skyy
                          New Member
                          • May 2007
                          • 109

                          #13
                          Originally posted by gits
                          try the following ;)

                          [CODE=javascript]file.addEventLi stener("click", function(event) { checkfilename2( event);}, false);[/CODE]

                          kind regards ...
                          Hi.. thanks alot.. i managed to solve the problem...

                          But yet another compliance problem to mozilla....

                          <SCRIPT language="JavaS cript">
                          function checkfname()
                          {
                          var upload_frm=docu ment.getElement ById('upload_fr m');
                          var len = upload_frm.elem ents.length;
                          var g=0;
                          for(g=0;g<len;g ++)
                          {
                          if(upload_frm.e lements[g].type=='file')
                          {
                          if(upload_frm.e lements[g].value!="")
                          {
                          var n=upload_frm.el ements[g].name;
                          var num=n.substr(8) ;
                          var fname=document. getElementById( 'fname'+num).va lue;
                          if(fname=="")
                          { alert("Please fill in a filename for each files selected."); return false;}
                          if(fname.indexO f("\/")>-1)
                          { alert("The '/' character is not allowed in the filename."); return false;}
                          }
                          }
                          }
                          return true;
                          }

                          Please help... thanks....

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5390

                            #14
                            ... what is not working? be more specific ... and show the call and from where it is called ...

                            kind regards ...

                            Comment

                            • skyy
                              New Member
                              • May 2007
                              • 109

                              #15
                              Originally posted by gits
                              ... what is not working? be more specific ... and show the call and from where it is called ...

                              kind regards ...

                              This code is used to check the filename for invalid names.. But its not working in mozilla...

                              The call is ..

                              <form action="test.cg i" enctype="multip art/form-data" method="post" name="upload_fr m" id="upload_frm " onsubmit="retur n checkfname()">

                              Comment

                              Working...