GIDForums  

Go Back   GIDForums > Webmaster Forums > Web Design Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Nov-2006, 03:53
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Review my Javascript


I have never taken the trouble to figure out Javascript, so this attempt reminds me of the days I used to hack together parts of perl scripts without really knowing what I was doing

Here's what I want to do, a user clicks on a checkbox and the action writes/appends the (option) ID to a cookie.

Details about the cookie:

Name: compare
Example cookie value: 23|5|90
Cookie expires: session only

The javascript I have put together:

Read the cookie

Code:
function gidReadCookie( name ) { var _COOKIE = null; var _COOKIES = document.cookie.split( ';' ); var matchName = name + "="; for( var i=0; i<_COOKIES.length; i++ ) { _COOKIE = _COOKIES[i]; while( _COOKIE.charAt(0)==' ' ) _COOKIE = _COOKIE.substring( 1, _COOKIE.length ); if( _COOKIE.indexOf(matchName)==0 ) return _COOKIE.substring( matchName.length, _COOKIE.length ); } return null; }

Write the cookie

Code:
/** * Writes a cookie. * * @param string name * @param string value * @param integer days 0 for session only. * @param string path * @return void */ function gidWriteCookie( name, value, days, path ) { var expires = "; expires="; if( days ) { var date = new Date(); date.setTime( date.getTime() + (days*24*60*60*1000) ); expires += date.toGMTString(); } else { expires += 0; } document.cookie = name + "=" + value + expires + "; path=" + path; }

Adding the ID to the cookie:

Code:
function addCompareID( id ) { id = id.toString(); var nameCookie = 'compare'; var newValue = null; var value = gidReadCookie( nameCookie ); if( value ) { var ids = value.split( '|' ); if( ids.length == 1 ) { if( ids != id ) ids.push( id ); else return; } else { if( ids.length >= 3 ) { alert( "The maximum number of plans you can compare at a time is 3" ); return; // TODO: unselect the option box. } else { if( -1 == ids.indexOf(id) ) ids.push( id ); else return; } } newValue = ids.join( '|' ); } else { newValue = id; } gidWriteCookie( nameCookie, newValue, 0, '/' ); }

If you can go through the code/script and suggest corrections, I will appreciate it. Ultimately, I just want them to work securely.
  #2  
Old 15-Nov-2006, 05:51
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Review my Javascript


Since starting this thread, I have refined the script further. Finally, I think I'm getting somewhere with JavaScript!

Here's an excerpt of the script that I uploaded today...

Code:
/** * Figure out the checkbox checked status for each hosting plan being (checked or unchecked) compared. * Depending on the action, the plan ID will be appended to or removed from the cookie. * * @param string cbid Input element (Checkbox) ID. * @param string id Web hosting plan ID. * @return void */ function comparisonCB( cbid, id ) { var nameCookie = 'compare'; if( document.getElementById(cbid).checked == true ) comparisonIDAdd( cbid, id, nameCookie, 3 ); else comparisonIDDel( cbid, id, nameCookie ); } /** * Add a web hosting plan ID to the cookie. Also stops adding beyond the maximum IDs allowed. * * @access private * @param string cbid Checkbox (input) ID. * @param string id Web hosting plan ID. * @param string nameCookie Name of cookie. * @param integer maxID Maximum IDs allowed. * @return void Writes a cookie. */ function comparisonIDAdd( cbid, id, nameCookie, maxID ) { var newValue = null; // new value to set the cookie at the end. var ids = loadComparisonIDs( nameCookie ); // an array of IDs. if( -1 == ids.indexOf(id) ) // only unique IDs accepted. { if( ids.length >= maxID ) { window.alert( "You may compare up to " + maxID + " web hosting plans at a time only." ); document.getElementById(cbid).checked = false; // unchecks this checkbox. return; // skip writing the cookie. } ids.push( id ); } else { return; // duplicate hosting plan ID, nothing to do. } newValue = ids.join( '|' ); gidWriteCookie( nameCookie, newValue, 0, '/' ); } /** * Delete a web hosting plan ID from the cookie. * * @access private * @param string cbid Checkbox (input) ID. * @param string id Web hosting plan ID. * @param string nameCookie Name of cookie. * @return void Writes a cookie. */ function comparisonIDDel( cbid, id, nameCookie ) { var newValue = null; // new value to set the cookie at the end. var ids = loadComparisonIDs( nameCookie ); // an array of IDs. var delIndex = ids.indexOf(id); if( -1 == delIndex ) return; else ids.splice( delIndex, 1 ); // removing this ID from the array. var expires = (newValue=ids.join('|')) ? 0 : -1; // delete the cookie if value is empty. gidWriteCookie( nameCookie, newValue, expires, '/' ); } /** * Reads domain cookies. * * @param string name Name of cookie to read. * @return string Value of cookie (or NULL). */ function gidReadCookie( name ) { var _COOKIE = null; var _COOKIES = document.cookie.split( ';' ); var matchName = name + "="; for( var i=0; i<_COOKIES.length; i++ ) { _COOKIE = _COOKIES[i]; while( _COOKIE.charAt(0)==' ' ) _COOKIE = _COOKIE.substring( 1, _COOKIE.length ); if( _COOKIE.indexOf(matchName)==0 ) return _COOKIE.substring( matchName.length, _COOKIE.length ); } return null; } /** * Writes a cookie. * * @param string name Name of cookie. * @param string value Value of cookie. * @param integer days 0 for session only. * @param string path * @return void */ function gidWriteCookie( name, value, days, path ) { var expires = "; expires="; if( days ) { var date = new Date(); date.setTime( date.getTime() + (days*24*60*60*1000) ); expires += date.toGMTString(); } else { expires += 0; } document.cookie = name + "=" + value + expires + "; path=" + path; } /** * Reads the cookie and converts the value, i.e. a string of IDs e.g. * <code>23|12|1009</code>, into an array. * * @access private * @param string nameCookie Cookie Name * @return array */ function loadComparisonIDs( nameCookie ) { var ids = new Array(); var value = gidReadCookie( nameCookie ); // reads the existing cookie value, if any. if( value ) ids = value.split( '|' ); return ids; }

If you can think of anything that makes this script more secure, anything at all, please do not hesitate to reply with your comment(s).

Meanwhile, I will continue my work on the "comparison" web pages to go with this script.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
We're looking for JavaScript codes / mini tutorials. JdS Web Design Forum 8 23-Nov-2008 04:27
An Introduction to Javascript JasonMichael Web Design Forum 2 24-Oct-2004 11:19
JavaScript Tutorial Part 1 pcxgamer Web Design Forum 2 01-Dec-2003 10:16
Before posting a web host review - PLEASE READ JdS Web Host Review Forum 0 16-Oct-2003 10:28
This JavaScript doesn't work on Mozilla. JdS Web Design Forum 8 02-Jul-2003 15:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.