What is causing the error `string.split is not a function`?

Eric picture Eric · Apr 13, 2012 · Viewed 323.1k times · Source

Why am I getting...

Uncaught TypeError: string.split is not a function

...when I run...

Answer

user1106925 picture user1106925 · Apr 13, 2012

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.