function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gautamgautam 

javascript to read and compare cookies

Hi all,

 

I have a requirement where i need read all existing cookies and comapre with a particular cookie(say x) in visualforce using javascript. If that particular cookie exist ,then on clicking a button it should go to url A, if that cookie does not exist, it should go to url b(login screen).

 

Sample code which is not working as of now.

<apex:page showHeader="false" sidebar="false" >

<b> Hello !!</b>

<p> You have logged in as {!$User.FirstName} {!$User.LastName}</p> <br/><br/>

<p> Your profile is {!$Profile.Name}..</p><br/><br/>

<script>

 

function Get_Cookie() {

    // first we'll split this cookie up into name/value pairs

    // note: document.cookie only returns, not the other components

    var a_all_cookies = document.cookie.split( ';' );

    var a_temp_cookie = '';

    var cookie_name = '';

    var cookie_value = '';

    var b_cookie_found = false; // set boolean t/f default f

    var i = '';

 

    for ( i = 0; i < a_all_cookies.length; i++ )

    {

        // now we'll split apart each pair

        a_temp_cookie = a_all_cookies[i].split( '=' );

 

 

        // and trim left/right whitespace while we're at it

        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

 

        // if the extracted name matches passed check_name

        if ( cookie_name == 'abcd' )

        {

            b_cookie_found = true;

            // note that in cases where cookie is initialized but no value, null is returned

            return cookie_value;

            alert('found it');

            window.location =  "http://www.google.com";

            break;

        }

        a_temp_cookie = null;

        cookie_name = '';

 

    }

    if ( !b_cookie_found )

    {

        return null;

        window.location = "https://www.yahho.com";

    }

}

 

</script>

 

 

<INPUT value="click" onclick="Get_Cookie()" />

</apex:page>

bob_buzzardbob_buzzard

In what way is the code not working?

 

You seem to have returns prior to the code that actually sets the window location.  Is this intentional?

gautamgautam

When i click on the button, it's not going to the url based on cookie javascript.

I just need to navigate to the urls, based on cookie's existence.

 

Please let me know what i need to modify to make this code work

bob_buzzardbob_buzzard

Coming back to this over a year later in case anybody hits this thread while searching.

 

The problem here is in this piece of code:

 

    // if the extracted name matches passed check_name
        if ( cookie_name == 'abcd' )
        {
            b_cookie_found = true;
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            alert('found it');
            window.location =  "http://www.google.com";
            break;
        }

 In that the javascript returns due to the 'return cookie_value' before the window.location value is changed to the desired URL.