HighDots Forums  

Re: simple error?

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: simple error? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: simple error? - 09-02-2005 , 11:36 AM






Geoff Cox <geoff.cox (AT) notquitecorrectfreeuk (DOT) com> writes:

Quote:
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).

Quote:
SCRIPT language="JavaScript"
Should be <script type="text/javascript">

Quote:
!--
Not necessary.

Quote:
function display_questions()
{
var questions= new Array(5)
Here "questions" is declared as a local variable inside the
"display_questions" function. The "questions" variable is only
visible inside this function.

Quote:
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.

Quote:
function again()
{
var i=0;
for (i = 0; i<5; i++)
A little shorter:
for (var i = 0; i < 5; i++)

Quote:
{
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.

Quote:
input type="button" value="see questions"
onclick="display_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 - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #2  
Old   
Geoff Cox
 
Posts: n/a

Default Re: simple error? - 09-02-2005 , 12:55 PM






On Fri, 02 Sep 2005 17:36:16 +0200, Lasse Reichstein Nielsen
<lrn (AT) hotpop (DOT) com> wrote:


Quote:
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">data</div>"

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

Cheers

Geoff




Reply With Quote
  #3  
Old   
web.dev
 
Posts: n/a

Default Re: simple error? - 09-02-2005 , 02:07 PM




Geoff Cox wrote:
Quote:
On Fri, 02 Sep 2005 17:36:16 +0200, Lasse Reichstein Nielsen
lrn (AT) hotpop (DOT) 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">data</div>"

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

Cheers

Geoff
Hi Geoff,

This is how it would work:


function display_questions()
{
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.getElementById("d_name").innerHTML = 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_questions()
{
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.getElementById("d_name").innerHTML = output;
}
Hope this helps.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.