Link to home
Start Free TrialLog in
Avatar of thenthorn1010
thenthorn1010Flag for United States of America

asked on

How to Write A Cookie In JavaScript to Make an Attribute Appear

I am new to web development, and I am having some trouble writing to a cookie using javascript from within HTML. I am attempting to make a division tag and its elements appear if the value of the cookie's view property are set to true. Once the close option is clicked, the cookie changes the value to false and should not allow a user to be able to view the elements within the division tag again. I have spent the past 8 hours trying to use the attached code, with no luck. Any help would be greatly appreciated.)
function setCookie2(c_name, value, exdays) {
            var exdate = new Date();
            var PolicyNum = "<%=strPolicyNumber%>";
            exdate.setDate(exdate.getDate() + exdays);
            var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
            document.cookie = "Policy=" + PolicyNum + "; " + c_name + "=" + c_value;
        }

        function getCookie2() {
            var c_name = "DontShowMsg";
            var i, x, y, ARRcookies = document.cookie.split(";");
            var PolicyNum = "<%=strPolicyNumber%>";
            for (i = 0; i < ARRcookies.length; i++) {
                x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
                y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
                x = x.replace(/^\s+|\s+$/g, "");

                if (x == "Policy" && y == PolicyNum) {
                    //hide_msg();
                }
                else {
                    alert(x + y);
                    show_msg();
                }

            }
        }

        function perm_close(){
            hide_msg();
            setCookie2("DontShowMsg", "Yes", "365");

        }
        
        function hide_msg() {
            document.getElementById("warnings").style.display = "none";
        }

        function show_msg() {
            document.getElementById("warnings").style.display = "block";

        }

Open in new window

Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Do you realize that the user will still be able to see the contents of the div by viewing source, and that they must allow cookies. and that you should be handling this server side by simple not writing the information if the user has previously seen it.


Cd&

Are you sure this value is being written correctly to the client?

I don't see the link, and you have not posted client side code so there is no way for me to check it myself.

Cd&
Avatar of thenthorn1010

ASKER

COBOLdinosaur,

I apologize for forgetting to attach the remaining code. Attached is the code. Let me know if you have any questions. THanks again for the help.

<div id="warnings">
                <div id="warningMess">
                                <h2>Click here for an important notice about Quicken Health Expense Tracker</h2>
                </div>
    <div id="warningClose">
                <h3>Don’t show this again</h3>
    </div>
    <div id="warningCloseBt">
                <img src="../MHP/WarningCloseButton.png" />
    </div>
</div>

Open in new window

#warnings {
                -moz-border-radius:10px;
    -webkit-border-radius:10px;
    border-radius:10px;
    border: 1px #ff686b solid;                            
    background: -moz-linear-gradient(19% 75% 90deg, #f87273, #feb4b5);
    background: -webkit-gradient(linear, left top, left bottom, from(#f87273), to(#feb4b5));
                background: -ms-linear-gradient(top, #ffb6b6, #fb8b8b);
                background: -o-linear-gradient(top, #ffb6b6, #fb8b8b);
    background-color: #fb9899;
                width:718px;
                height:34px;
                margin:0 0 0 19px;
}

#warnings h2{
                font-family:Verdana, Geneva, sans-serif;
                font-size:12px;
                text-decoration:underline;
                padding:10px;
                margin:0;
}

#warnings h3{
                font-family:Verdana, Geneva, sans-serif;
                font-size:11px;
                font-weight:normal;
                text-decoration:underline;
                padding:10px 0 10px 10px;
                margin:0;
}

#warningMess{
                float:left;
                width:555px;
} 

#warningClose {
                float:left;
                position:relative;
}

#warningCloseBt {
                float:right;
                position:relative;
                padding:10px 10px 10px 3px;
}

Open in new window

I'm missing a connection here somewhere how and where are the functions being called? I see showmsg and hide masg in the get cookie function, but I don't see a call to the cookie functions, or any event fired from a click.


Cd&
COBOLdinosaur,

Attached is the code updated with the updated code for how I intend for the events to be called when a link is clicked on in the banner, along with the "Click here to close" and the "X" image. The banner is supposed to not display for users ever again, unless they clear their cookies, if the user clicks on "Click here to close" or the "X" image. (I was trying to attempt to do this using a cookie. The cookie has to be written as a unique value each time a different user on the users computer were to click on it because more than one user is "capable" of using their account and clicking on the event-driving actions.)

I hope the updated code and this explanation clears up any confusion. Thank you again for any help you can provide.  
<div id="warnings" style="display:none;">
                <div id="warningMess">
                   <h2><a href="javascript:showpopup()">Click here for an important notice about Quicken Health Expense Tracker</a></h2>
                </div>
                <div id="warningClose">
                    <h3><a href ="javascript:perm_close()">Don’t show this again</a></h3>
                </div>
                <div id="warningCloseBt">
                    <img src="/imm/WarningCloseButton.png" onclick="javascript:perm_close()" />
                </div>
            </div>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial