homepage Welcome to WebmasterWorld Guest from 54.163.139.36
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Problem with split() method of JavaScript
I get : s.split is not a function.
bpositive

10+ Year Member



 
Msg#: 340 posted 10:18 pm on Apr 3, 2003 (gmt 0)

Hello all,
Using client side JavaScript I am trying to get a part of any URL if it contains "www". Actually I like to get anything after that.

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.

 

tedster

WebmasterWorld Senior Member tedster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 340 posted 1:12 am on Apr 4, 2003 (gmt 0)

I don't quite get that error message, but isn't the pattern you want to test and split for www. (with a dot), rather than www?

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 340 posted 1:23 am on Apr 4, 2003 (gmt 0)

Split expects a delimiter or a number of characters that serve as a delimiter not a regular expression. Trying to split [webmasterworld.com...] on /www/i will not work since it does not contains that string.

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

Xuefer

10+ Year Member



 
Msg#: 340 posted 2:33 am on Apr 4, 2003 (gmt 0)

use: document.location.href

bpositive

10+ Year Member



 
Msg#: 340 posted 4:09 am on Apr 4, 2003 (gmt 0)

Thank you all for your comments so far.

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

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 340 posted 4:15 am on Apr 4, 2003 (gmt 0)

This will convert the URL object to a string object. I did it explicitly by creating a new string object.

I didnīt know that split could split on regular expressions as well. But then my JavaScript is quite rusty as I havenīt been using it for a couple of years.

Andreas

bpositive

10+ Year Member



 
Msg#: 340 posted 4:21 am on Apr 4, 2003 (gmt 0)

Ah-ha! It makes sense now. THANK YOU.

Be Positive

Xuefer

10+ Year Member



 
Msg#: 340 posted 6:13 am on Apr 4, 2003 (gmt 0)

why not try what i said?
both document.location.href and document.location.toString() works

document.location+""
will convert document.location "toString()" automaticly

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved