Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 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; 09-08-2011 at 12:02 AM.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,917
    Thanks
    56
    Thanked 545 Times in 542 Posts
    which bit do you want, and will it always be the same part of the url?

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts
    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; 09-08-2011 at 12:45 AM.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    <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; 09-08-2011 at 12:51 AM.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,917
    Thanks
    56
    Thanked 545 Times in 542 Posts
    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);	
    	}

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,917
    Thanks
    56
    Thanked 545 Times in 542 Posts
    damn, Davey - faster and better. kudos

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts
    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; 09-08-2011 at 01:52 AM.

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    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; 09-08-2011 at 02:13 AM.

  • #9
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts
    aha! .href!

    thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •