This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-10-10 17:59:47

owlman
Scratcher
Registered: 2010-09-15
Posts: 100+

Javascript Help

I'm just starting Javascript, and I'm writing a beginner program in  MS notepad and whenever I try to open it it gives me an error saying,
Line: 1
Char: 1
Error: Syntax error 
Code: 800A03EA
Source: Microsoft JScript compilation error

My script is:

Code:

<!DOCTYPE html>
<html>
<body>
<script>
function changeImage()
{
element=document.getElementById('myimage')
if (element.src.match("bulbon"))
  {
  element.src="pic_bulboff.gif";
  }
else
  {
  element.src="pic_bulbon.gif";
  }
}function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>

<img id="myimage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">

<p id="demo">Click the light bulb to turn on/off the light</p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>
</html>

Last edited by owlman (2012-10-10 18:17:57)

Offline

 

#2 2012-10-10 19:44:15

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Javascript Help

The problem is that I don't think the !DOCTYPE tag allows 'html' in it, try without it.

Offline

 

#3 2012-10-10 20:12:48

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Javascript Help

TorbyFork234 wrote:

The problem is that I don't think the !DOCTYPE tag allows 'html' in it, try without it.

<!DOCTYPE html> is fine, it declares a html5 document.

I see though that your function declaration is incorrect.

var changeImage = function() {

statement;
statement;

};

Don't forget the semicolon right and the end either.


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#4 2012-10-10 20:47:16

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: Javascript Help

jji7skyline wrote:

TorbyFork234 wrote:

The problem is that I don't think the !DOCTYPE tag allows 'html' in it, try without it.

<!DOCTYPE html> is fine, it declares a html5 document.

I see though that your function declaration is incorrect.

var changeImage = function() {

statement;
statement;

};

Don't forget the semicolon right and the end either.

You can declare it his way too...


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#5 2012-10-10 20:53:48

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Javascript Help

MathWizz wrote:

jji7skyline wrote:

TorbyFork234 wrote:

The problem is that I don't think the !DOCTYPE tag allows 'html' in it, try without it.

<!DOCTYPE html> is fine, it declares a html5 document.

I see though that your function declaration is incorrect.

var changeImage = function() {

statement;
statement;

};

Don't forget the semicolon right and the end either.

You can declare it his way too...

You're right! I wonder what's the difference  hmm


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#6 2012-10-10 21:00:50

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Javascript Help

Ok, I've made a html of your code, then put images for the bulbon and bulboff, and it works fine.

Here's the code I used.

Code:

<!DOCTYPE html>
<html>
<body>
<script>
function changeImage()
{
element=document.getElementById('myimage')
if (element.src.match("bulbon"))
  {
  element.src="pic_bulboff.jpg";
  }
else
  {
  element.src="pic_bulbon.jpg";
  }
}function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>

<img id="myimage" onclick="changeImage()"
src="pic_bulboff.jpg" width="100" height="180">

<p id="demo">Click the light bulb to turn on/off the light</p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>
</html>

Notice I have pic_bulbon.jpg, since I used jpgs from Google Images for testing it.

Last edited by jji7skyline (2012-10-10 21:01:24)


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#7 2012-10-10 21:04:04

owlman
Scratcher
Registered: 2010-09-15
Posts: 100+

Re: Javascript Help

Ok, I'll try that.

Offline

 

#8 2012-10-10 21:06:25

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: Javascript Help

I also fiddled around with it a bit... http://jsfiddle.net/Kz4fj/
Note: Mine might be a little more crazy for a beginner. I got carried away. xD


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#9 2012-10-10 21:06:32

owlman
Scratcher
Registered: 2010-09-15
Posts: 100+

Re: Javascript Help

It gave me the exact same error, even with the exact same error code and stuff.

Offline

 

#10 2012-10-10 21:08:37

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Javascript Help

owlman wrote:

It gave me the exact same error, even with the exact same error code and stuff.

Your Microsoft compiler is wrong. Plus, JS doesn't need to be compiled, it's an interpreted language.

Just make a html file of your code and go  tongue


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#11 2012-10-10 21:15:52

owlman
Scratcher
Registered: 2010-09-15
Posts: 100+

Re: Javascript Help

jji7skyline wrote:

owlman wrote:

It gave me the exact same error, even with the exact same error code and stuff.

Your Microsoft compiler is wrong. Plus, JS doesn't need to be compiled, it's an interpreted language.

Just make a html file of your code and go  tongue

It works but the bulb images are showing up as the broken image icon.

Offline

 

#12 2012-10-10 21:32:00

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Javascript Help

Try replacing your images links with these.

Bulb on: http://i.imgur.com/5mNC1.jpg

Bulb off: http://i.imgur.com/gQFXG.jpg


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#13 2012-10-10 21:37:26

owlman
Scratcher
Registered: 2010-09-15
Posts: 100+

Re: Javascript Help

Nevermind! I got it to work and I saved it as a .hta so It doesn't open on the internet. Now let my JavaScript adventures begin!

Offline

 

#14 2012-10-10 21:51:34

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Javascript Help

Congrats! What exactly is a .hta file?  hmm


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#15 2012-10-11 10:49:27

transparent
Scratcher
Registered: 2011-04-19
Posts: 1000+

Re: Javascript Help

A quick search gave me this.


yes, yes i do.

Offline

 

#16 2012-10-11 16:52:58

owlman
Scratcher
Registered: 2010-09-15
Posts: 100+

Re: Javascript Help

jji7skyline wrote:

Congrats! What exactly is a .hta file?  hmm

I don't know but it works.

Offline

 

Board footer