javascript for capturing filename

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sharad Gupta

    #1

    javascript for capturing filename

    i have this problem of capturing the filename on the instance when
    onclick is activated in the <body>
    the function should catch the filename and display it.

    Second problem, i have to catch the image name in the same fashion
    using =onclick showimage(this) . Is this possible at at
    I have tried all combinations of
    document.getEle mentbyId();
    document.getEle mentbyName();
    document.getEle mentbyTagName() ;
    document.img[].src();

    pls help
  • McKirahan

    #2
    Re: javascript for capturing filename

    "Sharad Gupta" <g_sharad@yahoo .com> wrote in message
    news:7e985414.0 411160858.5c382 416@posting.goo gle.com...[color=blue]
    > i have this problem of capturing the filename on the instance when
    > onclick is activated in the <body>
    > the function should catch the filename and display it.
    >
    > Second problem, i have to catch the image name in the same fashion
    > using =onclick showimage(this) . Is this possible at at
    > I have tried all combinations of
    > document.getEle mentbyId();
    > document.getEle mentbyName();
    > document.getEle mentbyTagName() ;
    > document.img[].src();
    >
    > pls help[/color]

    Will this help? Watch for word-wrap.

    <html>
    <head>
    <title>show.htm </title>
    <script type="text/javascript">
    function showfile() {
    var pathname = location.pathna me;
    var filename =
    pathname.substr (pathname.lastI ndexOf("\\")+1, pathname.length );
    alert(filename) ;
    }
    function showpath() {
    var pathname = location.pathna me;
    pathname = pathname.substr (1,pathname.len gth);
    alert(pathname) ;
    }
    function showimage(that) {
    alert(that.name );
    }
    </script>
    </head>
    <body>
    <input type="button" value="Path+Fil e" onclick="showpa th()">
    <input type="button" value="File Only" onclick="showfi le()">
    <img src="http://www.google.com/images/logo.gif" name="Google"
    onclick="showim age(this)">
    </body>
    </html>


    Comment

    • Sharad Gupta

      #3
      Re: javascript for capturing filename

      Wow that solved my problem to the gr8 extent.

      What I wanted exactly is that the script function should only pick the
      primary name of the filename. Example.. if the file name is we1234.xyz
      The function should pick we1234 and display on alert.

      Pls help

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • news.west.cox.net

        #4
        Re: javascript for capturing filename

        > pathname.substr (pathname.lastI ndexOf("\\")+1, pathname.length );

        This is redundant.
        substr(startInd ex [,endIndex]).
        endIndex is optional.
        If endIndex is omitted, the substr goes to the end.

        To get rid of your extention try this.
        filename = pathname.substr (pathname.lastI ndexOf("\\")+1,
        pathname.lastIn dexOf("."));

        I don't remember off hand if substr includes the character at lastIndex or
        not. If so, subtract one from the endIndex argument.
        filename = pathname.substr (pathname.lastI ndexOf("\\")+1,
        pathname.lastIn dexOf(".")-1);


        Comment

        • Sharad Gupta

          #5
          Re: javascript for capturing filename

          this has solved my problem. Either when we do alert or pass the result
          to some div or span, it shows the primary name of the filename.

          function showfile(that) {
          var pathname = that.src;
          var filename =
          pathname.substr (pathname.lastI ndexOf("/")+1,pathname.l ength);
          document.getEle mentById("disp" ).innerText = filename;


          Thanks for your help


          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Sharad Gupta

            #6
            Re: javascript for capturing filename

            Sharad Gupta <g_sharad@yahoo .com> wrote in message news:<419a8ed8$ 0$14526$c397aba @news.newsgroup s.ws>...[color=blue]
            > Wow that solved my problem to the gr8 extent.
            >
            > What I wanted exactly is that the script function should only pick the
            > primary name of the filename. Example.. if the file name is we1234.xyz
            > The function should pick we1234 and display on alert.
            >
            > Pls help
            >
            > *** Sent via Developersdex http://www.developersdex.com ***
            > Don't just participate in USENET...get rewarded for it![/color]


            The prev ex. solved the problem, with some changes done.
            But still I can have the filename as we1234.xyz & not as we1234, what
            I want to achieve. Actually I want to give the primary part of
            filename as the name of the picture, so that I donr have to put in the
            picture name again n again for 1000's of them. So i found the result
            thru

            var filename = pathname.substr (pathname.lastI ndexOf("/")+1,pathname.l ength);
            document.getEle mentById("disp" ).innerText = filename;

            this gives filename(primar y.ext), but I want only primary name.
            thanks and waiting for reply

            Comment

            • Mick White

              #7
              Re: javascript for capturing filename

              Sharad Gupta wrote:

              [snip][color=blue]
              >
              > var filename = pathname.substr (pathname.lastI ndexOf("/")+1,pathname.l ength);
              > document.getEle mentById("disp" ).innerText = filename;
              >
              > this gives filename(primar y.ext), but I want only primary name.
              > thanks and waiting for reply[/color]

              var filename = pathname.split( ".");
              document.getEle mentById("disp" ).innerHTML = filename[filename.length-1]
              Mick

              Comment

              • Sharad Gupta

                #8
                Re: javascript for capturing filename

                hi

                thru the code i am able to generate the filename extension and not the
                primary name of the filename.

                ie we1234.xyz

                i am getting xyz, where as i want we1234

                still waiting


                *** Sent via Developersdex http://www.developersdex.com ***
                Don't just participate in USENET...get rewarded for it!

                Comment

                • denisb

                  #9
                  Re: javascript for capturing filename

                  Sharad Gupta <g_sharad@yahoo .com> wrote:[color=blue]
                  > thru the code i am able to generate the filename extension and not the
                  > primary name of the filename.
                  > ie we1234.xyz
                  > i am getting xyz, where as i want we1234[/color]

                  function showfile(that) {
                  var pathname = that.src;
                  var filename =
                  pathname.substr (pathname.lastI ndexOf("/")+1,pathname.l ength);
                  var filename = filename.substr (filename,filen ame.lastIndexOf ("."));
                  document.getEle mentById("disp" ).innerText = filename;
                  }

                  works with "lulu.lili_lolo .jpg"
                  ............... ..^


                  --
                  @@@@@
                  E -00 comme on est very beaux dis !
                  ' `) /
                  |\_ =="

                  Comment

                  • Mick White

                    #10
                    Re: javascript for capturing filename

                    Sharad Gupta wrote:
                    [color=blue]
                    >
                    > ie we1234.xyz
                    >
                    > i am getting xyz, where as i want we1234
                    >
                    > still waiting
                    >[/color]

                    var filename = pathname.split( ".");
                    document.getEle mentById("disp" ).innerHTML = filename[filename.length-2]

                    "length-2"
                    Mick

                    Comment

                    • Mick White

                      #11
                      Re: javascript for capturing filename

                      Sharad Gupta wrote[color=blue]
                      >
                      > ie we1234.xyz
                      >
                      > i am getting xyz, where as i want we1234
                      >[/color]


                      var filename = pathname.split( "/");
                      document.getEle mentById("disp" ).innerHTML =
                      filename[filename.length-1].split(".")[0]


                      Disregard earlier answer.
                      Mick

                      Comment

                      • McKirahan

                        #12
                        Re: javascript for capturing filename

                        "Sharad Gupta" <g_sharad@yahoo .com> wrote in message
                        news:419bacc2$0 $14502$c397aba@ news.newsgroups .ws...[color=blue]
                        > hi
                        >
                        > thru the code i am able to generate the filename extension and not the
                        > primary name of the filename.
                        >
                        > ie we1234.xyz
                        >
                        > i am getting xyz, where as i want we1234
                        >
                        > still waiting[/color]

                        Were you waiting for me to respond?

                        Here's a variation of what I posted before.

                        Watch for word-wrap.

                        <html>
                        <head>
                        <title>show.htm </title>
                        <script type="text/javascript">
                        function showfile() {
                        var pathname = location.pathna me;
                        var filename =
                        pathname.substr (pathname.lastI ndexOf("\\")+1, pathname.length );
                        alert(filename) ;
                        }
                        function showpath() {
                        var pathname = location.pathna me;
                        pathname = pathname.substr (1,pathname.len gth);
                        alert(pathname) ;
                        }
                        function showpref() {
                        var pathname = location.pathna me;
                        var filename =
                        pathname.substr (pathname.lastI ndexOf("\\")+1, pathname.length );
                        filename = filename.substr (0,filename.ind exOf("."))
                        alert(filename) ;
                        }
                        function showimage(that) {
                        alert(that.name );
                        }
                        </script>
                        </head>
                        <body>
                        <input type="button" value="Path+Fil e" onclick="showpa th()">
                        <input type="button" value="File Only" onclick="showfi le()">
                        <input type="button" value="File Pref" onclick="showpr ef()">
                        <img src="http://www.google.com/images/logo.gif" name="Google"
                        onclick="showim age(this)">
                        </body>
                        </html>


                        Comment

                        • Sharad Gupta

                          #13
                          Re: javascript for capturing filename

                          Thanks a lot..
                          it really solved my problem... other than this i also choked a function
                          with help of subStr and str.length.





                          *** Sent via Developersdex http://www.developersdex.com ***
                          Don't just participate in USENET...get rewarded for it!

                          Comment

                          Working...