PDA

View Full Version : Customizing a random quote script



AlterPoser
05-19-2009, 02:45 AM
Okay I need help with this script I found, I'm trying to customize it so members who sign in don't have to manually enter there name. I really don't know anything about javascript so any help would be great.

SITE (http://findthecrew.ning.com)


<script language="JavaScript" type="text/javascript">

//Comments Section
//Jim Allen email: tjallen@pipeline.com
//Random Greeting Generator and its html and javascript code are
//copyright 2001 by Thomas James Allen

//script to generate random greetings
function greetMe(){
//check to see if personalized
var cookievalue = getCookie('getyourwebsitehere');
if (cookievalue == null){firstname = ""}else{firstname = cookievalue};
//preload greetings
var say = new Array();
say[0] = "&nbsp;Welcome8";
say[1] = "&nbsp;Hello8";
say[2] = "&nbsp;Good to see you again8";
say[3] = "&nbsp;What a pleasure it is to see you8";
say[4] = "&nbsp;7Having a good time, we hope.";
say[5] = "&nbsp;7Thanks for visiting.";
say[6] = "&nbsp;Oh aren't you the bomb8";
say[7] = "&nbsp;You are totally cool8";
say[8] = "&nbsp;You da bomb8";
say[9] = "&nbsp;We should do lunch sometime8";
say[10] = "&nbsp;Yo8";
say[11] = "&nbsp;Greetings8";
say[12] = "&nbsp;Come on in8";
say[13] = "&nbsp;Lovely to see you again8";
say[14] = "&nbsp;7You made it.";
say[15] = "&nbsp;7We're glad you're here.";
say[16] = "&nbsp;Salutations8";
say[17] = "&nbsp;Hope you'll be staying for lunch8";
say[18] = "&nbsp;We are graced by your presence8";
say[19] = "&nbsp;Hi8";
say[20] = "&nbsp;Hi, how are you8";
say[21] = "&nbsp;Have a nice visit8";
say[22] = "&nbsp;Que pasa8";
say[23] = "&nbsp;Come in, come in8";
say[24] = "&nbsp;7You are thriving, I see.";
say[25] = "&nbsp;7Seems like so long since we'd seen you.";
say[26] = "&nbsp;We open our doors to you8";
say[27] = "&nbsp;Stay a while8";
say[28] = "&nbsp;Mouse right this way8";
say[29] = "&nbsp;I\'ll tell the boss you\'re here8";

//pick a random greeting
var howmany = 30
var bRand = 0;
bRand = Math.random();
bRand = Math.floor(bRand * howmany);
//prepare the cookievalue
if(firstname != ""){sayPers1 = ", " + firstname + ". "}else{sayPers1 = "."};
if(firstname != ""){sayPers2 = firstname + ", "}else{sayPers2 = ""};
//prepare and docwrite the greeting
sayWhat = say[bRand];
sayWhat = sayWhat.replace("8",sayPers1);
sayWhat = sayWhat.replace("7",sayPers2);
sayWhat = "<b>" + sayWhat + "<\/b>"
document.write(sayWhat);
document.close()
}

//below are the cookiemaker scripts
//this sets any cookies to expire one year after today (in milliseconds!)
var expiration = new Date();
expiration.setTime(expiration.getTime() + 24*60*60*365*1000);

//function for retrieving a cookie.
function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}

//function for setting a cookie.
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
return true;
}

//function for deleting a cookie.
function delCookie (name,path,domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}

//function to grab info from the cookiemaker form
function grabInfo(){
var cookinfo = document.reggy.info.value;
return cookinfo;
}

//-->


</script><script type="text/javascript">


<!--
greetMe();
//-->


</script>
<form name="reggy" onsubmit="return setCookie('getyourwebsitehere" id="reggy">
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td align="right"><b><font face="Arial, Helvetica, sans-serif">Your Name:</font></b></td>
<td><input type="text" name="info" size="20" ></input></td>
</tr>
<tr>
<td colspan="2">
<center>
<p class="buttongroup"><input type='submit' class="button" name="ss3" value='Enter' ></input></p>
</center>

I didn't really want to mess around with it 'cause I know even one small mistake can just throw everything off.

rangana
05-20-2009, 01:34 AM
What are you trying to do? You want to show a random message everytime your member visits your page?

This is better done with PHP using the rand() function.

How are you showing the member's name?

Nile
05-20-2009, 01:59 AM
Yes, rangana is right, here this should do half of it:


<?php
$random = array('One','Two','Three');
if(!logged_in()){
echo $random[rand(0,count($random))];
}
?>


Change the highlighted to the function/variable that says if the user is logged in or not.

AlterPoser
05-20-2009, 05:00 AM
o-O What? Well I wanted the greetings that flickr has when you log in, so I looked around and found THIS (http://getyourwebsitehere.com/jswb/randgreet.html) but you have to manually enter your name for it to work. I'm using Ning Networks (http://ning.com) and instead of having my members manually enter their names I want it to recognize their user account name when they log in. . .I think I just made this a whole lot more complicated.

Ughz

Nile
05-20-2009, 11:48 AM
Does your host support PHP? And how are you logging in to your site? PHP? Free login script?

Medyman
05-20-2009, 01:34 PM
I'm using Ning Networks (http://ning.com) and instead of having my members manually enter their names I want it to recognize their user account name when they log in. . .I think I just made this a whole lot more complicated.

Does Ning allow you to enter your own code? I played around with Ning a while ago and don't remember that being an option. The most they let you do was edit the CSS.

AlterPoser
05-21-2009, 08:40 AM
Does Ning allow you to enter your own code? I played around with Ning a while ago and don't remember that being an option. The most they let you do was edit the CSS.

Well they allow you to enter codes threw pages, content boxes, and the analytics section. I think that's pretty much it as for the PHP, I have no clue.

codeexploiter
05-21-2009, 12:44 PM
In a JS way



var say = ["Quote1", "Quote2", "Quote3"]
alert(say[Math.floor(Math.random() * say.length)]);