是什么导致了错误的字符串。分裂不是一个函数?

时间:2021-09-26 03:09:03

Why am I getting...

为什么我得到……

Uncaught TypeError: string.split is not a function

未捕获TypeError:字符串。分割不是函数

...when I run...

…当我运行……

(function () {
  var string = document.location;
  var split = string.split('/');
})();

4 个解决方案

#1


132  

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.

这是因为文档。location是一个location对象。tostring()将以字符串形式返回位置,因此连接将触发这个。


You could also use document.URL to get a string.

您还可以使用文档。URL获取一个字符串。

#2


45  

maybe

也许

string = document.location.href;
arrayOfStrings = string.toString().split('/');

assuming you want the current url

假设您想要当前url

#3


7  

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.toString()或document.location.href

#4


3  

document.location isn't a string.

文档。位置不是一个字符串。

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

您可能想要使用document.location。href或document.location。路径名。

#1


132  

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.

这是因为文档。location是一个location对象。tostring()将以字符串形式返回位置,因此连接将触发这个。


You could also use document.URL to get a string.

您还可以使用文档。URL获取一个字符串。

#2


45  

maybe

也许

string = document.location.href;
arrayOfStrings = string.toString().split('/');

assuming you want the current url

假设您想要当前url

#3


7  

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.toString()或document.location.href

#4


3  

document.location isn't a string.

文档。位置不是一个字符串。

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

您可能想要使用document.location。href或document.location。路径名。