Web Analytics Made Easy -
StatCounter splitting url so I can use the last part - CodingForum

Announcement

Collapse
No announcement yet.

splitting url so I can use the last part

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • splitting url so I can use the last part

    I'm trying to figure out how to split a url variable... I tried these two ways, with an alert to see my result, but I'm not getting anything. Suggestions?

    Code:
            function() {
    
                myURL = "http://www.test.com/folder1/folder2/someone"
    
               var partsA = myURL.split("//");
               var parts = partsA[1].split("/");
    
               alert(parts[2]);
    
            }

    Code:
    
    
    function() {
    
           myURL = "http://www.test.com/folder1/folder2/someone"
    
                var parts = myURL.split('/');
                var result = parts[parts.length-2] + '/' + parts[parts.length-1];
    
               alert(result);
    
            }
    Last edited by turpentyne; Sep 7, 2011, 07:02 PM.

  • #2
    which bit do you want, and will it always be the same part of the url?

    Comment


    • #3
      all I want is the last part: "someone". I know what I wrote would pull more than that, but I'm just testing to get it to work.

      The rest i don't need, but the folder names might change. Probably the same number of folders every time though.
      Last edited by turpentyne; Sep 7, 2011, 07:45 PM.

      Comment


      • #4
        <body>
        <div id = "d"> </div>
        </body>
        <script>

        function doit() {

        myURL = "http://www.test.com/folder1/folder2/someone"

        var parts = myURL.split('/');
        var result = parts[parts.length-1];

        alert(result);

        }
        doit()
        </script>


        always gives whatever is after last /
        Last edited by DaveyErwin; Sep 7, 2011, 07:51 PM.

        Comment


        • #5
          this gets the last bit of the url, split by /

          Code:
          function splitUrl() {
          	str="http://www.test.com/folder1/folder2/someone"
          	bits=str.split("/");
          	for (var a = 0; a < bits.length; a++) {
          	result=bits[bits.length-1];	
          		}
          		alert(result);	
          	}

          Comment


          • #6
            damn, Davey - faster and better. kudos

            Comment


            • #7
              I must be missing something...

              that script alone works fine, but I wasn't giving you the whole picture because I didn't think it was necessary. Maybe it is, because when I incorporate it, I don't get any result. Here's the script in complete form, with your version of the split. I have an alert in there just before it that shows theURL is correct, but then the rest can't seem to take it and split it:



              Code:
              <script>
              
              function doit() {
              
              
              var theURL = null;
              
                     var spans = document.getElementsByTagName("span");
                     for ( var s = 0; s < spans.length; ++s )
                     {
                         // searching through spans for the class name:
                         var span = spans[s];
                         if ( span.className.indexOf("posted-by") >= 0 )
                         {
                             // here's the right one
                             // getting href from inside that span:
                             var useit = span.getElementsByTagName("a")[0];
                             // and now the image link:
                             theURL = useit;
                             break; // just the one
                         }
                      }
                      if ( theURL == null ) 
                      {
                          alert("failed!");
                      } else {
              
                             alert(theURL); // just an alert to see if it's working to here
              
                             var parts = theURL.split('/');
              var result = parts[parts.length-1];
              
              alert(result); // this is the alert that isn't appearing for me.
              
              }
              }
              </script>  testing<br /> <input onclick="javascript:doit();return false;" type="button" value="--" /> <br />
              Last edited by turpentyne; Sep 7, 2011, 08:52 PM.

              Comment


              • #8
                Code:
                <script>
                
                function doit() {
                
                
                var theURL = null;
                
                       var spans = document.getElementsByTagName("span");
                       for ( var s = 0; s < spans.length; s++ )
                       {
                           // searching through spans for the class name:
                           var span = spans[s];
                           if ( span.className.indexOf("posted-by") >= 0 )
                           {               
                               var useit = span.getElementsByTagName("a")[0];              
                               theURL = useit.href;
                               break; // just the one
                           }
                        }
                        if ( theURL == null ) 
                        {
                            alert("failed!");
                        } else {
                
                
                
                               alert(theURL); // just an alert to see if it's working to here
                
                               var parts = theURL.split('/');
                var result = parts[parts.length-1];
                
                alert(result);
                
                }
                }
                </script>  
                testing<br /> <input onclick="javascript:doit();return false;" type="button" value="--" /> <br /><span class="posted-by"><a href="http://www.test.com/folder1/folder2/someone"></a></span>
                Because eveything in javascript(well almost everything) is an object
                and objects in native code usually have a tostring method
                that is envoked whenever you use the object as a string
                but not when refferencing a method , generally.
                Last edited by DaveyErwin; Sep 7, 2011, 09:13 PM.

                Comment


                • #9
                  aha! .href!

                  thanks!

                  Comment

                  Working...
                  X
                  😀
                  🥰
                  🤢
                  😎
                  😡
                  👍
                  👎