473,749 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple error?

Hello,

I am trying to print out the array values for a second time but get
error on page message?

Thanks

Geoff

<html>
<HEAD>

<SCRIPT language="JavaS cript">
<!--
function display_questio ns()
{
var questions= new Array(5)
questions[0]="question 1";
questions[1]="question 2";
questions[2]="question 3";
questions[3]="question 4";
questions[4]="question 5";
var x=0;
for (x=0; x<5; x++)
{
document.write( questions[x] + "<br>");
}
document.write( "<input type='button' value='again' onclick='again( )' /
");


}

function again()
{
var i=0;
for (i = 0; i<5; i++)
{
document.write( this.questions[i] + "<br>");
}

}
</SCRIPT>

</HEAD>
<BODY>

<input type="button" value="see questions"
onclick="displa y_questions()" />
</BODY>
</html>

Sep 2 '05 #1
18 1809
Hi Geoff,
The problem is when you did "document.write " you actually cleared
the function "again" itself. Thats why it complains "again is
notdefined".
Solutions:
1. Write "again" function again using document.write( ).
[OR]
2. Dont use document.write, instead write dynamically in a "div" or
"span".

- Peroli Sivaprakasam

Geoff Cox wrote:
Hello,

I am trying to print out the array values for a second time but get
error on page message?

Thanks

Geoff

<html>
<HEAD>

<SCRIPT language="JavaS cript">
<!--
function display_questio ns()
{
var questions= new Array(5)
questions[0]="question 1";
questions[1]="question 2";
questions[2]="question 3";
questions[3]="question 4";
questions[4]="question 5";
var x=0;
for (x=0; x<5; x++)
{
document.write( questions[x] + "<br>");
}
document.write( "<input type='button' value='again' onclick='again( )' /
");


}

function again()
{
var i=0;
for (i = 0; i<5; i++)
{
document.write( this.questions[i] + "<br>");
}

}
</SCRIPT>

</HEAD>
<BODY>

<input type="button" value="see questions"
onclick="displa y_questions()" />
</BODY>
</html>


Sep 2 '05 #2
On 2 Sep 2005 01:57:24 -0700, "Peroli" <pe****@gmail.c om> wrote:
Hi Geoff,
The problem is when you did "document.write " you actually cleared
the function "again" itself. Thats why it complains "again is
notdefined".
Solutions:
1. Write "again" function again using document.write( ).
[OR]
2. Dont use document.write, instead write dynamically in a "div" or
"span".


Peroli,

Could you please give me an idea what you mean by the second option?!

Cheers

Geoff

Sep 2 '05 #3
Geoff Cox <ge*******@notq uitecorrectfree uk.com> writes:
I am trying to print out the array values for a second time but get
error on page message?
What error message do you get? (If you use IE, you should enable
error messages when doing development).
<SCRIPT language="JavaS cript">
Should be <script type="text/javascript">
<!--
Not necessary.
function display_questio ns()
{
var questions= new Array(5)
Here "questions" is declared as a local variable inside the
"display_questi ons" function. The "questions" variable is only
visible inside this function.
document.write( "<input type='button' value='again' onclick='again( )' /
");

You seem to be trying to document.write an XHTML element (the closing
"/>" looks like XHTML), but document.write generally doesn't work
for XHTML pages parsed as such.

Anyway, when this button is clicked, the again function is called.
function again()
{
var i=0;
for (i = 0; i<5; i++)
A little shorter:
for (var i = 0; i < 5; i++)
{
document.write( this.questions[i] + "<br>");
This is called through a user click on a button. That suggests that the
page is already done loading when this is called. Doing "document.write "
on a page after it has finished loading will erase the entire page, and
replace it with what is written.

Also, the "this" operator refers to the global object (aka "window"),
which doesn't have a "questions" property.
<input type="button" value="see questions"
onclick="displa y_questions()" />


And this calls the first function, which also erases the page.

Generally, document.write is not the way to add content to a page
that is already loaded. There are different ways to do that, either
through the W3C DOM or using the proprietary "innerHTML" property.
<URL:http://jibbering.com/faq/#FAQ4_15>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 2 '05 #4
On Fri, 02 Sep 2005 17:36:16 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com > wrote:

Generally, document.write is not the way to add content to a page
that is already loaded. There are different ways to do that, either
through the W3C DOM or using the proprietary "innerHTML" property.
<URL:http://jibbering.com/faq/#FAQ4_15>


Lasse,

Thanks for your comments - how does using

"<div ID='"d_name">da ta</div>"

work when I try to write out the 5 array values?

Cheers

Geoff
Sep 2 '05 #5

Geoff Cox wrote:
On Fri, 02 Sep 2005 17:36:16 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com > wrote:

Generally, document.write is not the way to add content to a page
that is already loaded. There are different ways to do that, either
through the W3C DOM or using the proprietary "innerHTML" property.
<URL:http://jibbering.com/faq/#FAQ4_15>


Lasse,

Thanks for your comments - how does using

"<div ID='"d_name">da ta</div>"

work when I try to write out the 5 array values?

Cheers

Geoff


Hi Geoff,

This is how it would work:
function display_questio ns()
{
var questions= new Array(5)
questions[0]="question 1";
questions[1]="question 2";
questions[2]="question 3";
questions[3]="question 4";
questions[4]="question 5";

var str_questions = "";

for (var i=0; i < question.length ; ++i)
{
str_questions += questions[i] + "<br>";
}

document.getEle mentById("d_nam e").innerHTM L = str_questions;
}

OR even better yet, take the following shortcut if all you're doing is
adding the same string at the end.

function display_questio ns()
{
var questions= new Array(5)
questions[0]="question 1";
questions[1]="question 2";
questions[2]="question 3";
questions[3]="question 4";
questions[4]="question 5";

var output = questions.join( "<br>");

document.getEle mentById("d_nam e").innerHTM L = output;
}
Hope this helps. :)

Sep 2 '05 #6
On Fri, 02 Sep 2005 08:00:07 GMT, Geoff Cox
<ge*******@notq uitecorrectfree uk.com> wrote:

web dev

Many thanks for your reply. I have tried the first approach and that
works fine but probably better now if I try to explain what I am
trying to do!

The code below works OK up to a point but the function saveIt2() is
wrong and my original post was supposed to help me understand why!

The aim is to present an image and a series of questions which the
user answers by positioning the slider. I want to put the slider value
into an array and then when the last question has been answered the
next click of the Next button will send the array values to me by
email. To try to simplify things I am just trying to print out the
array values using saveIt2(().

Can you see what is going wrong with saveIt2()?

Cheers

Geoff

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<HEAD>

<link rel=stylesheet href="slider.cs s" type="text/css">
<SCRIPT SRC="slider.js" ></SCRIPT>
<SCRIPT SRC="prototype-1.3.1.js"></SCRIPT>

<SCRIPT>

var mySlider1 = new Slider( "Slider1" );

var count = 0;
var slider_value = new Array(5)

mySlider1.leftV alue = 0;
mySlider1.right Value = 10;
mySlider1.defau ltValue = 0;

mySlider1.offse tX = 1;
mySlider1.offse tY = 1;
mySlider1.maxSl ide = 258;

mySlider1.onmou seover = "self.status='T ry me!'";
mySlider1.onmou sedown = "self.status='Y ou start
at'+this.getVal ue(0)";
mySlider1.onmou seup = "self.status='Y ou end at '+this.getValue (0)";
mySlider1.oncha nge = "self.status='T he current value
is'+this.getVal ue(0)";
mySlider1.oncli ck = "this.setValue( this.defaultVal ue)";

mySlider1.oncha nge
="document.getE lementById('Sli der1ValueText') .innerHTML
=''+this.getVal ue(0)";

var lhs_questions = new Array(5)
lhs_questions[0]="LHS question 1";
lhs_questions[1]="LHS question 2";
lhs_questions[2]="LHS question 3";
lhs_questions[3]="LHS question 4";
lhs_questions[4]="LHS question 5";

var rhs_questions = new Array(5)
rhs_questions[0]="RHS question 1";
rhs_questions[1]="RHS question 2";
rhs_questions[2]="RHS question 3";
rhs_questions[3]="RHS question 4";
rhs_questions[4]="RHS question 5";

function buildTable()
{
document.write( "<center>") ;
document.write( "<table border='2'>");
document.write( "<tr>");
document.write( " <td ID='lhs_questio n'>" + this.lhs_questi ons[0] +
"</td>");
document.write( " <td>" + "<IMG SRC='sliderbg.g if'
NAME='Slider1Ra ilImg' ID='Slider1Rail Img'>" + "</td>");
document.write( " <td ID='rhs_questio n'>" + this.rhs_questi ons[0] +
"</td>");
document.write( "</tr>");
document.write( "</table>");
document.write( "</center>");
}

function next_question()
{
slider_value[count] = this.mySlider1. getValue(0);
this.count++;

if (this.count < 5)
{
document.getEle mentById('lhs_q uestion').inner HTML =
this.lhs_questi ons[count];
document.getEle mentById('rhs_q uestion').inner HTML =
this.rhs_questi ons[count];
} else
{
saveIt2();
}
}
function saveIt()
{
var url = 'http://path/formmail.cgi';
var pars =
'sliderVal='+do cument.getEleme ntById('Slider1 ValueText').inn erHTML;
var myAjax = new Ajax.Updater('S tatus', url, {method: 'post',
parameters: pars});

}

function saveIt2()
{
for (var i = 0; i <5; i++)
{
document.write( this.slider_val ue[i] + "<br>");
}
}

</script>

</HEAD>

<BODY onLoad="mySlide r1.placeSlider( )">

<h2 align="center"> Social Group</h2>

<p align="center"> <img src="pic3.jpg"> </p>

<script language="javas cript"
type="text/javascript">bui ldTable();</script>

<center>
<table>
<tr>
<td colspan="3" align="center">
<input type="button" value="Next" onclick="next_q uestion()" />

<SPAN class="invisibl e" ID="Slider1Valu eText"></SPAN>
<SPAN ID="Status"></SPAN></td></tr>

</TABLE>
</center>

<SCRIPT>

mySlider1.write Slider();

</SCRIPT>

</body>
</html>

Sep 2 '05 #7
hi Geoff,

function saveIt2()
{
for (var i = 0; i <5; i++)
{
document.write( this.slider_val ue[i] + "<br>");
}
}


Again the problem is with document.write( ). Please avoid it if you
don't know what you are doing. Try writing in a new "div" or "span".

[UNTESTED]
<div id="mydiv"></div>

function saveIt2()
{
for (var i = 0; i <5; i++)
{
document.getEle mentById('mydiv ').innerHTML += this.slider_val ue[i] +
"<br>";
}
}

- Peroli Sivaprakasam

Sep 3 '05 #8
On 3 Sep 2005 00:24:35 -0700, "Peroli" <pe****@gmail.c om> wrote:

Again the problem is with document.write( ). Please avoid it if you
don't know what you are doing. Try writing in a new "div" or "span".

[UNTESTED]
<div id="mydiv"></div>

function saveIt2()
{
for (var i = 0; i <5; i++)
{
document.getEle mentById('mydiv ').innerHTML += this.slider_val ue[i] +
"<br>";
}
}
Thanks Peroli - think I will get the O'Reilly Book on JavaScript -
seems to be well thought of?

Cheers

Geoff


- Peroli Sivaprakasam


Sep 3 '05 #9
On 3 Sep 2005 00:24:35 -0700, "Peroli" <pe****@gmail.c om> wrote:

Peroli,

I have used your code but for some reason I am getting the last value
of the array repeated at the beginning in the dat sent via email - can
you see why?

Geoff

function saveIt()
{
for (var i = 0; i < slider_value.le ngth; ++i)
{
document.getEle mentById("Slide r1ValueText").i nnerHTML +=
this.slider_val ue[i] + " ";
}

var situation = "Social Group";
var url = 'http://website/path/cgi-bin/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' ' +
'Slider_Values= '+document.getE lementById('Sli der1ValueText') .innerHTML;
var myAjax = new Ajax.Updater('S tatus', url, {method: 'post',
parameters: pars});
}
Sep 3 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1299
by: Gavin Bauer | last post by:
My DOS window (running in windows ME) closes the second it finishes running my programs. As you can imagine, this makes it hard to see the results. I've gotten in the habit of putting raw_input("Press enter to exit") at the end of every program, and in addition to being pain in the butt, it often fails to work. Being new to programming in general, I make more mistakes than most people. My programs often have errors before they get to my...
0
7112
by: mjcsfo | last post by:
I can't seem to find a reference nor any helpful threads on this topic. I've gotten the following error in two circumstances: 1. A complex type has nested within it another complex type, in the "Russian doll" style of schema design, where the nested type is local and anonymous (not a separate global type). 2. A complex type has nested within it a simple type which is derived through restriction with at least one facet, again nested,...
16
2906
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after the preload has been completed.
5
6055
by: Lili | last post by:
I'm having problems creating a simple stored procedure in DB2. Can someone help? Here is the screen dump when I tried to load the stored procedure. Thanks for any help. Create procedure update_salary (in emp_number char(6), in rate integer) language sql begin update employee
5
7245
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline in the input when I use fgets(). Now I have looked around on the net, not so much in this group...
2
5183
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then sends the message "Ping" to the server, which reads it and answers with a "Pong". The game is really simple and the coding should be also very simple! But for me it isn't. By the way, the program uses datagram sockets (UDP). And, I'm using
8
1727
by: Bern McCarty | last post by:
I have a simple ref class in its own namespace that needs to coexist with a legacy typedef alias for "unsigned int" in the global namespace that has the identifier as itself. Everything compiles fine with the old MEC++ syntax, but I cannot figure out how to write the code so that it will compile in C++/CLI. Can someone tell me how? Here is the code in both syntaxes: // This MEC++ code compiles just fine with VC8 using cl -c...
1
8139
by: tomer.ha | last post by:
Hi there, I'd like to send emails from a Python program using Simple MAPI. I've tried this code: http://mail.python.org/pipermail/python-list/2004-December/298066.html and it works well with Outlook Express 6 and Thunderbird 1.5, but it doens't work at all with Microsoft Outlook 2007. I keep getting this message: "WindowsError: MAPI error 2". I don't want to use Extended MAPI because it doesn't support thunderbird not OE. Therefore,...
5
2819
by: adam.timberlake | last post by:
I've just finished reading the article below which goes into some depth about exceptions. The article was rather lucid and so I understand how to implement it all, the thing I'm having trouble with though is why would I need to them? For example, I could suppress my functions using the @ sign and then use an if statement to check if they returned false or not. Similarly, I can also return various integers, like -4 and -5 for errors, and...
6
28872
kenobewan
by: kenobewan | last post by:
Congratulations! You are one of the few who realise that over 80% of errors are simple and easy to fix. It is important to realise this as it can save a lot of time. Time that could be wasted making unnecessary changes, that in turn can cause further problems. Programming is a humbling experience. An experience that causes one to reflect on human error. One major cause of these errors is syntax, syntax, syntax. We tend not to notice when we...
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.