By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,946 Members | 1,610 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,946 IT Pros & Developers. It's quick & easy.

Choosing A Random Name Issues...

P: n/a
Code is suppose to choose a random name using 'event listeners'. The
reference file is a file downloaded from the book's publisher "Sitepoint". I
doubt that there is anything wrong with the file itself. There has to be
something wrong with the code or my referencing of the file (the file is in
the same directory as the code). Not sure, but here's the code:

<html><head><title>Prob.4</title>
<style type="text/css">
#textfield {position:absolute; left:0; top: 15;}
</style>

<script type="text/javascript" src="core.js"></script>
<script type="text/javascript">
var RandomName=
{

init:function()
{
var form=document.getElementsByTagName("form")[0];
var my_button=form.firstChild;
Core.addEventListener(my_button,"click", RandomName.chooseNumber);
},

chooseNumber:function()
{
var ran_Num=parseInt(Math.random()*5);

RandomName.chooseName(ran_Num);
},

chooseName:function(my_num)
{
var names=new Array();
names[0]="Moe";
names[1]="Larry";
names[2]="Curly";
names[3]="James";
names[4]="John";
var new_window=window.open("","new_window," "height=100, width=100");
new_window.src=names[my_num];
var text_div=document.getElementById("textfield");
var message="I think the class feels that"+names[my_num]+should answer
the question";
text_div.innerHTML=message;

}
};

Core.start(RandomName);
</script></head>
<body>
<div id="textfield"></div>
<form>
<input type="button" value="Choose a name">
</form>
</body>
</html>

--
Message posted via http://www.webmasterkb.com

Aug 18 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
There are two obvious errors in the code...

var new_window=window.open("","new_window," "height=100, width=100");

Should be ...

var new_window=window.open("","new_window", "height=100, width=100");

...and...

var message="I think the class feels that"+names[my_num]+should answer
the question";

Should be...

var message="I think the class feels that"+names[my_num]+"should
answer the question";

Jees, who writes this stuff?
Aug 18 '08 #2

P: n/a
Laser Lips wrote:
Thanks for your response.
>There are two obvious errors in the code...

var new_window=window.open("","new_window," "height=100, width=100");

Should be ...

var new_window=window.open("","new_window", "height=100, width=100");
where's the difference here?
>
var message="I think the class feels that"+names[my_num]+should answer
the question";

Should be...

var message="I think the class feels that"+names[my_num]+"should
answer the question";
okay, got it...another gramatical error...
>Jees, who writes this stuff?
--
Message posted via http://www.webmasterkb.com

Aug 18 '08 #3

P: n/a
>>There are two obvious errors in the code...
>>var new_window=window.open("","new_window," "height=100, width=100");
>>Should be ...
>>var new_window=window.open("","new_window", "height=100, width=100");
>where's the difference here?
Look again...

var new_window=window.open("","new_window," "height=100, width=100");

Should be ...

var new_window=window.open("","new_window", "height=100, width=100");

Aug 18 '08 #4

P: n/a
In comp.lang.javascript message <88d4ff5462bfe@uwe>, Mon, 18 Aug 2008
01:57:22, LayneMitch via WebmasterKB.com <u39402@uwe.?.invalidposted:
var ran_Num=parseInt(Math.random()*5);
Additionally, you should choose a better, more intelligent source to
copy from. Newsgroup FAQ 4.44 and link(s) refer.

Write less code at once, and test incrementally. Test with a browser
that displays at least reasonable error reporting - Firefox, Opera, for
example.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 18 '08 #5

P: n/a
Laser Lips wrote:
>
Look again...

var new_window=window.open("","new_window," "height=100, width=100");

Should be ...

var new_window=window.open("","new_window", "height=100, width=100");
Okay got that. And it's still not working. what the heck!?

All of the suggested updates have been made. Here's the new code:

<html><head><title>Prob.4</title>
<style type="text/css">
#textfield{position:absolute; left:0; top: 15;}
</style>

<script type="text/javascript" src="core.js"></script>
<script type="text/javascript">

var RandomName=
{

init:function()
{
var form=document.getElementsByTagName("form")[0];
var my_button=form.firstChild;
Core.addEventListener(my_button,"click", RandomName.chooseNumber);
},

chooseNumber:function()
{
var ran_Num=parseInt(Math.random()*5);

RandomName.chooseName(ran_Num);
},

chooseName:function(my_num)
{
var names=new Array();
names[0]="Moe";
names[1]="Larry";
names[2]="Curly";
names[3]="James";
names[4]="John";
var new_window=window.open("","new_window", "height=100, width=100");
new_window.src=names[my_num];
var text_div=document.getElementById("textfield");
var message="I think the class feels that"+names[my_num]+"should answer
the question";
text_div.innerHTML=message;

}
};

Core.start(RandomName);
</script></head>
<body>
<div id="textfield"></div>
<form>
<input type="button" value="Choose a name">
</form>
</body>
</html>

--
Message posted via http://www.webmasterkb.com

Aug 19 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.