Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:46:57+00:00 2026-06-17T14:46:57+00:00

document.cookie = happy=random stuff; function readCookie(name) { var cookie,cname,i; cookie = document.cookie.split(;); cname =

  • 0
document.cookie = "happy=random stuff";
function readCookie(name) {
    var cookie,cname,i;
    cookie = document.cookie.split(";");
    cname = name;
    for (i=0; i<=cookie.length; i++) {
        var c = cookie[i];
        if (c.indexOf(cname) == 0) {
            alert(c.substring(cname.length,c.length));
            return c.substring(cname.length,c.length);
        }
    }
    return null;
}

readCookie("happy");

http://jsfiddle.net/a2R3j/
It appears that this line is wrong:

if (c.indexOf(cname) == 0) {

it works perfectly. However jsfiddle says there is an error. Is there actually anything wrong?

EDIT FOR CLARITY: I want it to return the cookie’s content by calling the function getCookie(name here)
another edit for clarity:
Yusaf Code: stuff

Code with indexOf: stuff

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:46:58+00:00Added an answer on June 17, 2026 at 2:46 pm

    http://jsfiddle.net/6qLZe/

    use .split("=")[1] rather than your substring method

    document.cookie = "joker=batman";
    
    function readCookie(cname) {
        var cookie_split = document.cookie.split(";");
        for (i = 0; i < cookie_split.length; i++) {
            var cookie_var = cookie_split[i].split("=")[0];
            if (cookie_var == cname) {
                return cookie_split[i].split("=")[1];
            }
        }
        return null
    }
    
    alert(readCookie("joker"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function readCookie(name) { var ca = document.cookie.split(';'); var nameEQ = name + =; for(var
This is my code. It alert()'s NaN everytime. function updateScore(action){ var cookieArray = document.cookie.split(;);
I am using the following cookie: var $j = jQuery.noConflict(); $j(document).ready(function(){ if (document.cookie.indexOf('visited=true') ==
<script type=text/javascript> jQuery(document).ready(function(){ if (document.cookie.indexOf('visited=false') == -1) { var fifteenDays = 1000*60*60*24*30; var expires
jQuery(document).ready(function(){ if (document.cookie.indexOf('visited=true') === -1) { var expires = new Date(); expires.setDate(expires.getDate()+30); document.cookie =
EDIT: this works (gives me an alert): if (document.cookie.indexOf(TR_LNG) >= 0) { alert('cookie found');
I'm wondering if using the following Javascript code is reliable: if (!document.cookie) { alert('Cookies
I have a function that saves cookies cookievalue= escape(document.passwordCheck.oldPassword.value) + ;; document.cookie=oldCookie= + cookievalue
In javascript alert(document.cookie); does not show the .ASPXAUTH Cookie although a sniffer is showing
This is my JavaScript code I use to create my cookie............ document.cookie = Name=

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.