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

Why am I getting...

Uncaught TypeError: string.split is not a function

...when I run...

_x000D_
_x000D_
var string = document.location;_x000D_
var split = string.split('/');
_x000D_
_x000D_
_x000D_

This question is related to javascript jquery split

The answer is


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


In clausule if, use (). For example:

stringtorray = "xxxx,yyyyy,zzzzz";
if (xxx && (stringtoarray.split(',') + "")) { ...

document.location isn't a string.

You're probably wanting to use document.location.href or document.location.pathname instead.


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to split

Parameter "stratify" from method "train_test_split" (scikit Learn) Pandas split DataFrame by column value How to split large text file in windows? Attribute Error: 'list' object has no attribute 'split' Split function in oracle to comma separated values with automatic sequence How would I get everything before a : in a string Python Split String by delimiter position using oracle SQL JavaScript split String with white space Split a String into an array in Swift? Split pandas dataframe in two if it has more than 10 rows