我打开url http://localhost
,在其中加载js脚本。
在我的js脚本中执行:
'use strict';
var path = window.location.pathname;
console.log(path); // it prints /
var arr = path.split('/');
if (arr.length === 0) { //actual length is 2
console.log('test'); //not executed
}
所以没有打印test
,我的arr
包含两个元素,它们是空的。为什么数组的长度等于2?
发布于 2018-02-01 06:44:27
您位于/
(服务器根目录)中。如果在您的/
中使用split
作为分隔符,则将有n+1
元素,n
是字符串中分隔符的数量。
换句话说,您的结果是path
等于[THING 1]/[THING 2]
(两者都是空字符串),这为您提供了一个包含['', '']
的arr
。
arr
的长度为2,这两个元素都是空字符串。
https://stackoverflow.com/questions/48565316
复制相似问题