The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
The split() function can be used to split a given string or a line by specifying one of the substrings of the given string as the delimiter. The string before and after the substring specified as a delimiter is returned as the output.
split (separator, limit) , if the separator is not in the string, it returns a one-element array with the original string in it.
Note: The split() method does not change the original string. Remember – JavaScript strings are immutable. The split method divides a string into a set of substrings, maintaining the substrings in the same order in which they appear in the original string. The method returns the substrings in the form of an array.
Change this...
var string = document.location;
to this...
var string = document.location + '';
This is because document.location
is a Location object. The default .toString()
returns the location in string form, so the concatenation will trigger that.
You could also use document.URL
to get a string.
maybe
string = document.location.href;
arrayOfStrings = string.toString().split('/');
assuming you want the current url
run this
// you'll see that it prints Object
console.log(typeof document.location);
you want document.location.toString()
or document.location.href
document.location
isn't a string.
You're probably wanting to use document.location.href
or document.location.pathname
instead.
In clausule if, use ()
.
For example:
stringtorray = "xxxx,yyyyy,zzzzz";
if (xxx && (stringtoarray.split(',') + "")) { ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With