JavaScript Bug

Soldato
Joined
11 Apr 2003
Posts
4,208
Location
Notts
Hi all, posted this by mistake in general chat the other day, and asked for it to be moved, but as it has not.... Here we go again!

I am having a problem, where if I have my style switcher (Which sets a cookie) and my guestbook entry (Which sets a cookie) on the same page, the Style Switcher sets its cookie, but the guestbook does not.

The style style sheet setsa cookie to remeber which style is selected

The guesbook to stop people from keep posting..

help! thanks, jcb33

http://jcb33.co.uk/guestbook.php
 
You'd probably be better off having a serverside scripted guestbook...I see you're running PHP. There's loads of PHP guestbooks out there!

I say this because if the user has javascript disabled, they can't post on the guestbook! :eek:
 
toastyman said:
You'd probably be better off having a serverside scripted guestbook...I see you're running PHP. There's loads of PHP guestbooks out there!

I say this because if the user has javascript disabled, they can't post on the guestbook! :eek:
That is correct, I can validate my guestbook via php now however, so im going to be enabling the ability to post withabout js as well, however that Still does not solve my cookie setting bug :(

Going to stick with my current guestbook as I dont want a pre written one that is just stuck into your site
 
Well I cant work this out, My guestbook js:

Code:
function storeGuest(){
//-------------------
// This function stores the user details into a cookie called guest.

    var today = new Date()
    var expiry = new Date()

    expiry.setHours( today.getHours() + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 + 999 )


    firstField= document.forms[0].elements[0].value
    payload = firstField
    for (var i = 1; i < 3; i++) {
        fieldValue = document.forms[0].elements[i].value
        payload = payload + ":" + fieldValue
    }

    BD_SetCookie( 'guest', payload, expiry)
}

function showGuest(){
//------------------
// This function shows the enter entry if theres no cookie and doesnt if theres a cookie.

var guestCookie = BD_GetCookie('guest')

with (document) {

// if theres not a user cookie just display empty input boxes.
        if(! guestCookie){
			document.write('<form method="post" name="guestform" autocomplete="off" >\n'
				      + '<table width="773" border="0" cellspacing="0" cellpadding="0">\n'
				      + '  <tr>\n'
				      + '    <td width="90" align="left" valign="top" class="guestBookB">Message Title: </td>\n'
				      + '    <td width="683"><input name="title" type="text" maxlength="20" /></td>\n'
				      + '  </tr>\n'
				      + '  <tr>\n'
				      + '    <td align="left" valign="top" class="guestBookB">User: </td>\n'
				      + '    <td><input name="user" type="text" maxlength="20" /></td>\n'
				      + '  </tr>\n'
				      + '  <tr>\n'
				      + '    <td align="left" valign="top" class="guestBookB">Message: </td>\n'
				      + '    <td><textarea name="message" cols="60"/></textarea></td>\n'
				      + '  </tr>\n'
			          + '        </table>\n'
				      + ' <br/>\n'
					  + ' <input name="guestSubmit" value="Submit" onclick="return validate1(this.form)" type="submit"/>\n'
				      + ' <input type="reset" name="Reset" value="Reset Message" />\n'
				      + '   </form>\n'
				      + '     <hr/>\n')

            return false;    // to indicate no form started, no need to end it
        }
            
        else{ // else if there is a guest cookie.
        
		document.write('\n      <p>Thanks for your post!</p>\n')
        }
        }
      
}



//VALIDATION CODE

function validate1(form){
//-----------------------
// This function validates if any of the input boxes have a value in.

var bad = false

	if (form.title.value == ""){
		bad =true;
		form.title.focus();
	}
		
	else if (form.user.value == ""){
		bad =true;
		form.user.focus();
	} 
	
	else if (form.message.value == ""){
		bad =true;
		form.message.focus();
	} 
	
	
	if (form.title.value == "" ) {
		alert('Please Input the message title.')
		form.title.focus();
		return false;
	}
	
	if (form.user.value == "" ) {
		alert('Please input your user name.')
		form.user.focus();
		return false;
	}
	
	if (form.message.value == "" ) {
		alert('Please Input a message.')
		form.message.focus();
		return false;
	}
storeGuest()		
return (true)

}


//BILL DORTCH CODE

// code in the functions BD_GetCookie, BD_SetCookie() and BD_DeleteCookie
// is based on code written by Bill Dortch and placed in the public domain
// It was modified by Martin Webb and is available from
// <URL:http://tech.irt.org/articles/js016/index.htm>
// these functions are the accepted standard way of handling cookies in JScript

// The layout has been altered by Peter Scott, but nothing else has been
// interfered with!  Make sure you leave them alone too!!

function BD_GetCookie( name) {
//----------------------------
    var start = document.cookie.indexOf( name + '=')
    var len = start + name.length + 1
    if ((!start) && (name != document.cookie.substring(0,name.length)))
        return null
    if (start == -1)
        return null
    var end = document.cookie.indexOf( ';',len)
    if (end == -1)
        end = document.cookie.length
    return unescape( document.cookie.substring( len,end))
}

function BD_SetCookie( name, value, expires, path, domain, secure) {
//------------------------------------------------------------------
    document.cookie = name + '=' + escape( value)
        + ( (expires) ? ';expires=' + expires.toGMTString() : '')
        + ( (path) ? ';path=' + path : '')
        + ( (domain) ? ';domain=' + domain : '')
        + ( (secure) ? ';secure' : '')
}

function BD_DeleteCookie( name, path, domain) {
//---------------------------------------------
    if (BD_GetCookie( name))
        document.cookie = name + '='
            + ( (path) ? ';path=' + path : '')
            + ( (domain) ? ';domain=' + domain : '')
            + ';expires=Thu, 01-Jan-1970 00:00:01 GMT'
}

My styleswitcher js:

Code:
/*This Javascript is used to make drop-down menus work*/

function go() {
window.location=document.getElementById("menu").value
}

//Creates Style Switcher

function disShow(){

with (document) {
	document.write('Accessibility Options'
	+' '
	+'<form action="">'
	+' <select id="switcher" onchange="setActiveStyleSheet(this.options[this.selectedIndex].value); return false;">'
	+' <option value="default">--Style Changer--</option>'
	+' <option value="default">Default Style</option>'
	+' <option value="LargeFont">Large Font</option>'
    +' <option value="">Text Only</option>'
	+' </select>'
	+ '   </form>'
	+' ')
				         
	}
}

/*This Javascript is used by the CSS Switcher. Author: Paul Sowden, Taken From:http://alistapart.com/d/alternate/styleswitcher.js*/
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

*EDIT* Removing [code for (var i = 1; i < 3; i++) {
fieldValue = document.forms[0].elements.value
payload = payload + ":" + fieldValue
}[/code]

Seems to have fixed this... But now I have an error where if I enter a started tag in "title", e.g <a href="image">moo it breaks the page
 
Last edited:
Back
Top Bottom