3

I was trying to split this string through "##" delimitter.

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])

The history.length alert is giving me result as 6. But ideally it should be 2. The history[0] alert is giving undefined. Please help me with this as I am not able to get why this is happening.

7
  • 1
    Your code works for me on JSFiddle. It alerts me with 2 then with the text before the ##. Commented Jun 14, 2016 at 16:50
  • 1
    Even your snippet is working for me -- EDIT : When you click on Run Snippet several times, the first value increments Commented Jun 14, 2016 at 16:51
  • Yes, is there any way i can make sure that the js/jquery split function is called? Commented Jun 14, 2016 at 16:52
  • What kind of server are you using? In some cases (like Coldfusion) # is a special character and could be causing odd behaviors as a result. Commented Jun 14, 2016 at 16:52
  • Related: Using the variable “name” doesn't work with a JS object. Commented Jun 14, 2016 at 17:00

1 Answer 1

10

"History" (or even "history") is a defined by the browser and representing your history.

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

Just change the name of the history variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.