2016-06-14 52 views
3

我试图通过“##”分隔符来分割这个字符串。Js分割函数给出错误结果

var historycookie = "8.4707417,77.0463719:Sector 14:Gurgaon##28.3952729,77.3238274:Sector 15:Faridabad"; 
 
var history = historycookie.split("##"); 
 
alert(history.length);alert(history[0])

history.length警报给我造成为6 但理想应该是2 的history[0]警报是给不确定的。请帮助我,因为我无法弄清楚为什么会发生这种情况。

+1

你的代码适用于[JSFiddle](https://jsfiddle.net/fg8n36wq/)。它用2来提醒我,然后用'##'之前的文本提醒我。 –

+1

即使你的代码片段正在为我工​​作 - **编辑**:当你点击'Run Snippet'几次,第一个值增加 – tektiv

+0

是的,有没有什么办法可以确保js/jquery split功能是叫什么名字? –

回答

10

“历史”(或甚至“历史”)是由浏览器定义并代表您的历史。

history.length; // is returning size of entries in your history 
history[0]; // undefined, because it is not an array 

只需更改历史变量的名称即可。

+0

thanx Eytibi,你救了我.. –