...

View Full Version : splitting url so I can use the last part



turpentyne
09-07-2011, 11:58 PM
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?




function() {

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

var partsA = myURL.split("//");
var parts = partsA[1].split("/");

alert(parts[2]);

}








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);

}

xelawho
09-08-2011, 12:36 AM
which bit do you want, and will it always be the same part of the url?

turpentyne
09-08-2011, 12:40 AM
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.

DaveyErwin
09-08-2011, 12:48 AM
<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 /

xelawho
09-08-2011, 12:58 AM
this gets the last bit of the url, split by /



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);
}

xelawho
09-08-2011, 12:59 AM
damn, Davey - faster and better. kudos :thumbsup:

turpentyne
09-08-2011, 01:46 AM
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:





<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 />

DaveyErwin
09-08-2011, 01:59 AM
<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.

turpentyne
09-08-2011, 02:05 AM
aha! .href!

thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum