Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #18140 > unrolled thread

Why is this not working

Started by"Tony Johansson" <johansson.andersson@telia.com>
First post2013-01-19 17:35 +0100
Last post2013-01-20 18:20 +0000
Articles 18 — 10 participants

Back to article view | Back to comp.lang.javascript


Contents

  Why is this not working "Tony Johansson" <johansson.andersson@telia.com> - 2013-01-19 17:35 +0100
    Re: Why is this not working Tim Streater <timstreater@greenbee.net> - 2013-01-19 16:52 +0000
      Re: Why is this not working Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-19 18:45 +0100
    Re: Why is this not working Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-01-19 10:17 -0700
    Re: Why is this not working Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-19 18:26 +0100
      Re: Why is this not working Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-19 18:50 +0100
    Re: Why is this not working Richard Yates <richard@yatesguitar.com> - 2013-01-19 11:11 -0800
      Re: Why is this not working Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-19 21:17 +0100
        Re: Why is this not working Richard Yates <richard@yatesguitar.com> - 2013-01-19 14:30 -0800
          Re: Why is this not working Stefan Weiss <krewecherl@gmail.com> - 2013-01-20 02:52 +0100
            Re: Why is this not working Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-20 03:08 +0100
              Re: Why is this not working Stefan Weiss <krewecherl@gmail.com> - 2013-01-20 04:32 +0100
                Re: Why is this not working Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-20 04:46 +0100
    Re: Why is this not working Danny <dann90038@gmail.com> - 2013-01-19 12:42 -0800
      Re: Why is this not working "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-01-19 22:53 +0100
    Re: Why is this not working Jeff North <jnorthau@yahoo.com.au> - 2013-01-20 15:13 +1100
      Re: Why is this not working Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-20 06:45 +0100
    Re: Why is this not working Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-01-20 18:20 +0000

#18140 — Why is this not working

From"Tony Johansson" <johansson.andersson@telia.com>
Date2013-01-19 17:35 +0100
SubjectWhy is this not working
Message-ID<kdei12$7eg$1@dont-email.me>
Hello!
I'm quite now to javascript and wonder why is not the called function 
changinf the value for element with id=demo

<!DOCTYPE html>
<html>
<body>

<p id="demo">testing</p>

<script>
function myFunction(a,b)
{
   return a*b;
}

for (int i = 0; i< 10; i++)
document.getElementById("demo").innerHTML=myFunction(4,3);
</script>

</body>
</html> 

[toc] | [next] | [standalone]


#18141

FromTim Streater <timstreater@greenbee.net>
Date2013-01-19 16:52 +0000
Message-ID<timstreater-577109.16520219012013@news.individual.net>
In reply to#18140
In article <kdei12$7eg$1@dont-email.me>,
 "Tony Johansson" <johansson.andersson@telia.com> wrote:

> Hello!
> I'm quite now to javascript and wonder why is not the called function 
> changinf the value for element with id=demo
> 
> <!DOCTYPE html>
> <html>
> <body>
> 
> <p id="demo">testing</p>
> 
> <script>
> function myFunction(a,b)
> {
>    return a*b;
> }
> 
> for (int i = 0; i< 10; i++)
> document.getElementById("demo").innerHTML=myFunction(4,3);
> </script>
> 
> </body>
> </html> 

1) 12 is not html

2) your code is likely running before the page is finished loading

3) what is "int" ?

4) Why are you going round a ten-long loop? That'll be finished before 
you can blink.

5) Why did you put your code in the body?

Something like this may work:


<!DOCTYPE html>
<html>
<head>

<meta charset="UTF-8">

<script type="text/javascript">

function myFunction(a,b)
     {
     return a*b;
     }

function main ()
     {
     document.getElementById("demo").innerText = myFunction (4,3);
     }

</script>

</head>

<body onload="main();">

<p id="demo">testing</p>

</body>
</html>

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#18144

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-01-19 18:45 +0100
Message-ID<1561476.8OV0T9qb6M@PointedEars.de>
In reply to#18141
Tim Streater wrote:

> "Tony Johansson" <johansson.andersson@telia.com> wrote:
>> I'm quite now to javascript and wonder why is not the called function
>> changinf the value for element with id=demo
>> 
>> <!DOCTYPE html>
>> <html>
>> <body>
>> 
>> <p id="demo">testing</p>
>> 
>> <script>
>> function myFunction(a,b)
>> {
>>    return a*b;
>> }
>> 
>> for (int i = 0; i< 10; i++)
>> document.getElementById("demo").innerHTML=myFunction(4,3);
>> </script>
>> 
>> </body>
>> </html>
> 
> 1) 12 is not html

That is most certainly not the reason why it does not work.

The right-hand side of the assignment is most certainly being converted to 
String as the type of the “innerHTML” IDL attribute is DOMString, therefore 
String in DOM implementations. [1]

In any event, “12” *is* _HTML_.  It is just not an HTML *element*, but then 
again nobody said it needed to be (hint: *inner*HTML).
 
> 2) your code is likely running before the page is finished loading

That could be a reason why it does not work.
 
> 3) what is "int" ?

An (extra) identifier starting with ES 5.1 (_not_ a future reserved word as 
I thought, and as it was in ES 3, §7.5.3).  That is the primary reason why 
it does not work. [2]

______
[1] <https://dvcs.w3.org/hg/innerhtml/raw-file/tip/>, previously
    in <http://www.w3.org/TR/2008/WD-html5-20080610/dom.html#innerhtml>
[2] <http://ecma-international.org/ecma-262/5.1/#sec-7.6.1>,
    <http://ecma-international.org/ecma-262/5.1/#sec-12.6.3>
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18142

FromMartin Leese <please@see.Web.for.e-mail.INVALID>
Date2013-01-19 10:17 -0700
Message-ID<kdekg7$np0$1@dont-email.me>
In reply to#18140
Tony Johansson wrote:
> Hello!
> I'm quite now to javascript and wonder why is not the called function 
> changinf the value for element with id=demo

Lots of reasons:
1.  The <script> element embeds JavaScript, but does not execute it.
2.  The JavaScript must be embedded before it is executed.
3.  In JavaScript, there is no int statement.

Note that your for loop does not achieve anything.

Try:
<!DOCTYPE html>
<html>
<body>

<script>
function myFunction(a,b)
{
   return a*b;
}

function updateDemo()
{
   for (var i = 0; i< 10; i++)
     document.getElementById("demo").innerHTML=myFunction(4,3);
}
</script>

<p id="demo">testing</p>
<button type="button" onclick="updateDemo();">Update demo</button>

</body>
</html>

-- 
Regards,
Martin Leese
E-mail: please@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/

[toc] | [prev] | [next] | [standalone]


#18143

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-01-19 18:26 +0100
Message-ID<14011564.rDXmUOF8TW@PointedEars.de>
In reply to#18140
Tony Johansson wrote:

> I'm quite now to javascript
            ^^^
There is no “javascript” [1] and YSCIB.

> and wonder why is not the called function
> changinf the value for element with id=demo
  ^^^^^^^^
Please take more when writing your Usenet articles.
 
> <!DOCTYPE html>
> <html>
> <body>
> 
> <p id="demo">testing</p>
> 
> <script>
> function myFunction(a,b)
> {
>    return a*b;
> }

Learn pretty printing to make source code easily readable

  function myFunction (a, b)
  {
    return a * b;
  }
 
> for (int i = 0; i< 10; i++)
-------^
Syntax error.  “int” is a *future* reserved word; these languages are 
*dynamically* typed.  Perhaps you were looking for

> document.getElementById("demo").innerHTML=myFunction(4,3);

  for (var i = 0; i < 10; ++i)
  {
    document.getElementById("demo").innerHTML = myFunction(4, 3);
  }

Which begs the question: Why do you want to execute this 10 times?
Why do you expect the result of the first iteration to be different
from that of the fifth or the tenth?

Accessing an element before the document has been loaded, like this,
also is not reliably.  Move your code to a function that you call from

  <body … onload="…">
    …
  </body>

________
[1] <http://PointedEars.de/es-matrix>
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18145

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-01-19 18:50 +0100
Message-ID<3943237.YJimVgxblZ@PointedEars.de>
In reply to#18143
Thomas 'PointedEars' Lahn wrote:

> Tony Johansson wrote:
>> I'm quite now to javascript
>             ^^^
> There is no “javascript” [1] and YSCIB.
> 
>> and wonder why is not the called function
>> changinf the value for element with id=demo
>   ^^^^^^^^
> Please take more when writing your Usenet articles.

Ha, you can see here a Usenet law fulfilled.  I meant to say:

  Please take more *care* when writing your Usenet articles.

:)
  
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18146

FromRichard Yates <richard@yatesguitar.com>
Date2013-01-19 11:11 -0800
Message-ID<nurlf8ht64qtqghh478a7mjrtfqceknvfe@4ax.com>
In reply to#18140
<!DOCTYPE html>
<meta charset="UTF-8"><html>
<body>
<p id="demo">testing</p>
<script>
function myFunction(a,b) { return a*b;}
for (var  i = 0; i< 10; i++) 
document.write(myFunction(4,i)+'<br />'); 
</script>
</body>
</html> 

[toc] | [prev] | [next] | [standalone]


#18148

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-01-19 21:17 +0100
Message-ID<7309657.PzdHBbrMY6@PointedEars.de>
In reply to#18146
Richard Yates wrote:

> <!DOCTYPE html>
> <meta charset="UTF-8"><html>
> <body>
> <p id="demo">testing</p>
> <script>
> function myFunction(a,b) { return a*b;}
> for (var  i = 0; i< 10; i++)
> document.write(myFunction(4,i)+'<br />');
> </script>
> </body>
> </html>

While your code is syntactically correct, you are missing the point which 
was to modify the *content* of the “p” element.

And you have not quoted anything.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18151

FromRichard Yates <richard@yatesguitar.com>
Date2013-01-19 14:30 -0800
Message-ID<r37mf85tplqvog2pfi90cejhkqnje2pori@4ax.com>
In reply to#18148
On Sat, 19 Jan 2013 21:17:57 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>Richard Yates wrote:
>
>> <!DOCTYPE html>
>> <meta charset="UTF-8"><html>
>> <body>
>> <p id="demo">testing</p>
>> <script>
>> function myFunction(a,b) { return a*b;}
>> for (var  i = 0; i< 10; i++)
>> document.write(myFunction(4,i)+'<br />');
>> </script>
>> </body>
>> </html>
>
>While your code is syntactically correct, you are missing the point which 
>was to modify the *content* of the “p” element.

Since the original code seemed to be trying to modify the p element 10
times over in a flash too quick to see, it is not at all clear what
the point was. His loop had no point at all. My code shows him how to
make the loop and function work. That's here he has to start and from
there he can modify things to do whatever it is that actually is the
point. 

[toc] | [prev] | [next] | [standalone]


#18154

FromStefan Weiss <krewecherl@gmail.com>
Date2013-01-20 02:52 +0100
Message-ID<kdfikk$65r$1@news.albasani.net>
In reply to#18151
On 2013-01-19 23:30, Richard Yates wrote:
> modify the p element 10 times over in a flash too quick to see

In a typical browser, it's not just too fast to see, but impossible to
see: the display will only be updated (re-rendered) after the script has
run its course.

- stefan

[toc] | [prev] | [next] | [standalone]


#18155

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-01-20 03:08 +0100
Message-ID<1840215.6M2mspuZ93@PointedEars.de>
In reply to#18154
Stefan Weiss wrote:

> On 2013-01-19 23:30, Richard Yates wrote:
>> modify the p element 10 times over in a flash too quick to see
> 
> In a typical browser, it's not just too fast to see, but impossible to
> see: the display will only be updated (re-rendered) after the script has
> run its course.

The behavior is implementation-dependent; there is nothing ”typical”
about it.  And the word you were looking for was “reflow”.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18157

FromStefan Weiss <krewecherl@gmail.com>
Date2013-01-20 04:32 +0100
Message-ID<kdfogm$hg2$1@news.albasani.net>
In reply to#18155
On 2013-01-20 03:08, Thomas 'PointedEars' Lahn wrote:
> Stefan Weiss wrote:
>> In a typical browser, it's not just too fast to see, but impossible to
>> see: the display will only be updated (re-rendered) after the script has
>> run its course.
> 
> The behavior is implementation-dependent; there is nothing ”typical”
> about it.  And the word you were looking for was “reflow”.

This behavior is an optimization, so of course it's implementation
dependent. Practically all current JS-capable browsers display this
behavior, hence "typical". Prove that a significant percentage of
browsers work differently, and I'll retract "typical".

The word I intended is "rerender" (aka "repaint"). Reflows can happen
several times between display updates, but they are not visible to the
user until the display is updated (rerendered, repainted). Nothing in
the given example forces a reflow until the final display update.


- stefan


PS, didn't you killfile me in November "for a longer time"? Just in case
your filter got confused, please re-add me.

[toc] | [prev] | [next] | [standalone]


#18158

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-01-20 04:46 +0100
Message-ID<2010653.qWjjvXNv77@PointedEars.de>
In reply to#18157
Stefan Weiss wrote:

> On 2013-01-20 03:08, Thomas 'PointedEars' Lahn wrote:
>> Stefan Weiss wrote:
>>> In a typical browser, it's not just too fast to see, but impossible to
>>> see: the display will only be updated (re-rendered) after the script has
>>> run its course.
>> 
>> The behavior is implementation-dependent; there is nothing ”typical”
>> about it.  And the word you were looking for was “reflow”.
> 
> This behavior is an optimization, so of course it's implementation
> dependent. Practically all current JS-capable browsers display this
> behavior, hence "typical". Prove that a significant percentage of
> browsers work differently, and I'll retract "typical".

Your logic is flawed as you are trying to shift the burden of proof.  You 
were to prove that “practically all current JS-capable browsers display this 
behavior”, not I that this is not true.  Probably someone has told you this 
before.

> The word I intended is "rerender" (aka "repaint"). Reflows can happen
> several times between display updates, but they are not visible to the
> user until the display is updated (rerendered, repainted). Nothing in
> the given example forces a reflow until the final display update.

Doubtful.

> PS, didn't you killfile me in November "for a longer time"? Just in case
> your filter got confused, please re-add me.

So that you can have it your way and continue spouting nonsense?  No.
(New year, new scorefile rules.)

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18149

FromDanny <dann90038@gmail.com>
Date2013-01-19 12:42 -0800
Message-ID<1a0efdf3-08ad-4f58-8c9d-09a72b31193f@googlegroups.com>
In reply to#18140
To Tony Johansson

I assume you meant something akin to -> http://www.webdevout.net/test?0z&raw  maybe

[toc] | [prev] | [next] | [standalone]


#18150

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-01-19 22:53 +0100
Message-ID<XnsA14DE8DE79195eejj99@194.109.133.133>
In reply to#18149
Danny wrote on 19 jan 2013 in comp.lang.javascript:

> To Tony Johansson
> 
> I assume you meant something akin to ->
> http://www.webdevout.net/test?0z&raw  maybe 

Please email him or quote what you are talking about, this is Usenet.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#18159

FromJeff North <jnorthau@yahoo.com.au>
Date2013-01-20 15:13 +1100
Message-ID<17qmf898pvv7167pkqt4nvrchg0ojfso31@4ax.com>
In reply to#18140
On Sat, 19 Jan 2013 17:35:40 +0100, in comp.lang.javascript "Tony
Johansson" <johansson.andersson@telia.com>
<kdei12$7eg$1@dont-email.me> wrote:

>| Hello!
>| I'm quite now to javascript and wonder why is not the called function 
>| changinf the value for element with id=demo
>| 
>| <!DOCTYPE html>
>| <html>
>| <body>
>| 
>| <p id="demo">testing</p>
>| 
>| <script>
>| function myFunction(a,b)
>| {
>|    return a*b;
>| }
>| 
>| for (int i = 0; i< 10; i++)
        ---  change int to var. You can omit it altogether in this
instance
>| document.getElementById("demo").innerHTML=myFunction(4,3);
>| </script>
>| 
>| </body>
>| </html> 

The 'int' declaration is causing a syntax error and therefore the rest
of the code is not being executed.

Depending upon what browser that you are using, you can press the F12
key to bring up the developer toolbar. Have a look in the area labeled
console. This is where any javascript errors will be displayed.

[toc] | [prev] | [next] | [standalone]


#18160

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-01-20 06:45 +0100
Message-ID<2034240.CDkVJ4E7OL@PointedEars.de>
In reply to#18159
Jeff North wrote:

"Tony Johansson" wrote.
>>| for (int i = 0; i< 10; i++)

Will you *please* fix your quotation prefix?

>         ---  change int to var. You can omit it altogether in this
> instance

Wrong.  Not using “var” would make “i” a property of the Global Object at 
best.  In the worst case it causes a runtime error.

>>| document.getElementById("demo").innerHTML=myFunction(4,3);
>>| </script>
>>| 
>>| </body>
>>| </html>
> 
> The 'int' declaration is causing a syntax error and therefore the rest
> of the code is not being executed.

Because ”int” is not a keyword (at most it is a future reserved word), there 
is no declaration, just a syntax error.

> Depending upon what browser that you are using, you can press the F12
> key to bring up the developer toolbar. Have a look in the area labeled
> console. This is where any javascript errors will be displayed.

There is no “javascript” either.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#18176

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-01-20 18:20 +0000
Message-ID<mSbP$cCiVD$QFwXy@invalid.uk.co.demon.merlyn.invalid>
In reply to#18140
In comp.lang.javascript message <kdei12$7eg$1@dont-email.me>, Sat, 19
Jan 2013 17:35:40, Tony Johansson <johansson.andersson@telia.com>
posted:

>Hello!
>I'm quite now to javascript and wonder why is not the called function
>changinf the value for element with id=demo
>
><!DOCTYPE html>
><html>
><body>
>
><p id="demo">testing</p>
>
><script>
>function myFunction(a,b)
>{
>  return a*b;
>}
>
>for (int i = 0; i< 10; i++)
>document.getElementById("demo").innerHTML=myFunction(4,3);
></script>
>
></body>
></html>


Your REAL problem is that you have not understood the importance of
error messages, nor of giving full details.

You have not said what your operating system or executing agent are; we
can deduce that the code is written for a browser (which does not prove
that you are using one), but not which agent you are using, nor which
version.

You have, it seems, not considered - you have certainly not reported,
the error message that your agent should have given you - you may need
to open a window to see it.

Use a respectable browser, consider the error message, and the fault
should be obvious enough.  I suggest Firefox or Opera, or maybe Chrome.


But, as it is, it is an over-complicated test.  The "for" line serves no
useful purpose as you think it is; and commenting it out locates the
error to that line.

Never try to learn programming without first learning the relevant
debugging tools (or how to debug without them, which is slower).

-- 
 (c) John Stockton, nr London UK               Reply address via Home Page.
   news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
   <http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
   <http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web