Hi, all
I wonder if anybody could please explain this to me, I've stripped down the code to its basic functions to try and understand is myself, but, I still don't.
I'm trying to get the second option (which matches the second Regexp as I want it to) to return a random reply (1 of 3), but the random reply is not so random, every time I press test is returns the same string unless I refresh the browser (which I don't want to do). Do I need to write the random reply function differently or is it something else ?.
the sample code is,
Thanks for reading.
I wonder if anybody could please explain this to me, I've stripped down the code to its basic functions to try and understand is myself, but, I still don't.
I'm trying to get the second option (which matches the second Regexp as I want it to) to return a random reply (1 of 3), but the random reply is not so random, every time I press test is returns the same string unless I refresh the browser (which I don't want to do). Do I need to write the random reply function differently or is it something else ?.
the sample code is,
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<script type="text/javascript">
//<![CDATA[
var search_text=new Array();
var find_reply=new Array();
var found_res=new Array();
//
search_text[0]=/test1/i;
search_text[1]=/another test/i;
//
find_reply[0]="this is test 1";
find_reply[1]=rand_reply();
//
function _example(s){
var str;
for(var j=0;j<search_text.length;j++){
if(s.search(search_text[j])>-1){
str=find_reply[j];
alert(str);//alert out come
}
}
}
//
function rand_reply(){
found_res[0]="alert this";
found_res[1]="or alert this one";
found_res[2]="or, finally this";
return found_res[rdm(0,2)];
}
//
function rdm(x,y){return Math.round(Math.random()*(y-x))+x;}
//]]>
</script>
<title></title>
</head>
<body>
<form action="null" onsubmit="_example(document.getElementById('sample').value);return false;">
<select id="sample">
<option value="test1">test1</option>
<option value="another test">another test</option>
</select> <input type="submit" name="submit" value="test" /></form>
</body>
</html>
Thanks for reading.