Forum Moderators: open
Example:
Original URL: "http://www.webmasterworld.com/"
My final string should be: "webmasterworld.com/"
I found our that split() is the method I need. In this example it should return an array containing 2 elements: everything before and after www i.e.
[0] = http://
[1] = webmasterworld.com/
Here is my code:
<SCRIPT type="text/JavaScript">
<!--
// GET THE URL
var current_url = document.location;
// PATTERN WE ARE LOOKING FOR IN THE URL
re = /www/i;// i - CASE SENSITIVE
//! CHECK TO SEE IT IT EXISTS IN THE URL,
// TRUE=DO NOTHING, FALSE=EXTRACT AND MODIFY
if (!re.test (current_url)){
document.write("Does not exist")
}
else{
//REGULAR EXPRESSION HERE
document.write("Exists<br>");
s = current_url.split(re);
document.write(s[1]);
}
// -->
</SCRIPT>
For some reason it does not work. The error message I get is: s.split is not a function.
Help please.
Split is a method of the string object. Location returns a URI? object.
var myString = new String(document.location);
var myArray = myString.split('www.');
alert(myArray[1]);
will work.
Andreas
tedster,
Yes, you are right. I AM missing a dot there. But that is not the problem. Good eye.
Andreas,
I am pretty sure we can use regular expressions.(http://www.webreference.com/js/column5/methods.html).
The script you wrote works fine.
I also fixed my own code (with a little help from a frined).
We added a space at the end of the URL, when assigning to a var and it worked.
var current_url = document.location;
chaged to
var current_url = document.location + " ";
Now I wonder why? Any thoughts on that?
Be Positive