Avatar of redzoneglobal
redzoneglobal asked on

JS Error -> Split is not a function

I'm teaching myself javascript, as we have decided to develop our own analytics tracking to integrate into our analytics db.  We had previously been using a 3rd party to provide tracking technology.  So, this is my first dive into javascript, with my experience mostly in VB .net/asp and SQL.  I was making good progress, and then ran into this issue.  Javascript tested through: var broken_info = cookieValue.split(":")

When the browser executes this line I get: cookieValue.split is not a function in Firefox error console.

Here is the complete javascript (which is housed in a js file) and is called from the javascript at this URL:  http://www.searchmarketingtools.com/tester.html?source=testengine&kw=test+keyword

//<!-- Tracking Code v1.00 - All rights reserved -->

function Randomstring()
{
var chararr="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

var rndstr=""
for(i=0;i<10;i++)
{

      rndstr=rndstr+chararr.charAt(Math.round(Math.random()*62)-1)
}
    id = rndstr;
    return rndstr;
}
function Setcookie()
{
    var _d=new Date();
    _d.setTime(_d.getTime()+31536000000);
      expires=_d
    var fcnv = "SMTstatbuilder="+"id:"+Randomstring()+"; path=/; expires="+expires;
    document.cookie = fcnv;


}
function readCookie(name) {
      if (document.cookie == '') {
   // there's no cookie, so go no further
      return false;
      } else {
   // there is a cookie
      var firstChar, lastChar;
      var theBigCookie = document.cookie;
      firstChar = theBigCookie.indexOf(name);
   // find the start of 'name'
      if(firstChar != -1)  {
   // found cookie
      firstChar += name.length + 1;
   // skip 'name' and '='
      lastChar = theBigCookie.indexOf(';', firstChar);
   // Find the end of the value string (i.e. the next ';').
      if(lastChar == -1) lastChar = theBigCookie.length;
      return unescape(theBigCookie.substring(firstChar, lastChar));
            } else {
   // If there was no cookie of that name, return false.
      return false;
      }
    }
}
var id
var cookieValue = readCookie('SMTstatbuilder');
if(cookieValue == "false"){Setcookie}
else {
     var broken_info = cookieValue.split(":")
    id = broken_info[1];
}
******************************************************************
From all the reference/documentation I have reviewed, split is a valid string function.  Why am I getting this error?
JavaScript

Avatar of undefined
Last Comment
hielo

8/22/2022 - Mon
Russell2566

Your calling you function to create the cookie wrong, or I should say not at all.... Once I adjusted the set cookie function to be called all worked well....

if(cookieValue == false) {
    Setcookie();
} else {
    var broken_info = cookieValue.split(":",1)
    id = broken_info[1];
}
ASKER CERTIFIED SOLUTION
hielo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
redzoneglobal

Russell2566:
Even though you pointed out another syntax error on the setCookie function call, Hielo was correct with the assessment that I was testing for a string condition, instead of a boolean condition on the cookieValue variable. (I appreciate you catching that for me)..

Hielo,
Guess I was confused as cookieValue will contain string data, if the cookie already exists, and not boolean data value.  I'm still learning the nuances of javascript, but for the most part it's pretty straight forward.

Appreciate the quick replies from both of you!
hielo

in regards to: "cookieValue will contain string data, if the cookie already exists"
you are correct! The problem is that since the cookie was not being set correctly, the logic inside your readCookie function was returning false;
Look at the alert("?") in this example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function Randomstring()
{
var chararr="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

var rndstr=""
for(i=0;i<10;i++)
{

      rndstr=rndstr+chararr.charAt(Math.round(Math.random()*62)-1)
}
    id = rndstr;
    return rndstr;
}
function Setcookie()
{
    var _d=new Date();
    _d.setTime(_d.getTime()+31536000000);
      expires=_d
    var fcnv = "SMTstatbuilder="+"id:"+Randomstring()+"; path=/; expires="+expires;
    document.cookie = fcnv;


}
function readCookie(name)
{
      if (document.cookie == '')
       {
               // there's no cookie, so go no further
      return false;
      }
      else
      {
            // there is a cookie
            var firstChar, lastChar;
            var theBigCookie = document.cookie;
            firstChar = theBigCookie.indexOf(name);
               // find the start of 'name'
           if(firstChar != -1)  
            {
                  // found cookie
                  firstChar += name.length + 1;
                  // skip 'name' and '='
                  lastChar = theBigCookie.indexOf(';', firstChar);
                  // Find the end of the value string (i.e. the next ';').
                  if(lastChar == -1)
                        lastChar = theBigCookie.length;
                  return unescape(theBigCookie.substring(firstChar, lastChar));
            }
            else
            {
                  alert("?");
                  // If there was no cookie of that name, return false.
            return false;
            }
    }
}
var id
var cookieValue = readCookie('SMTstatbuilders');

if(cookieValue == "false"){Setcookie}
else {
     var broken_info = cookieValue.split(":")
    id = broken_info[1];
}
</script>
</body>
</html>

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck