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

#1 2012-05-06 15:33:34

PullJosh
Scratcher
Registered: 2011-08-01
Posts: 500+

HTML5/Javascript problem (again!)

I have plenty of problems with stuff like this, but this one's embarassing. I'm making an interactive tutorial about HTML5, and I'm having trouble with the coding.  tongue  I made a post on my forums about it. You can see it here.


http://www.blocks.scratchr.org/API.php?action=text&string=I'm_on_vacation!&xpos=155&ypos=90&font_size=30&bgimage=http://imageshack.us/a/img339/7215/sspeechsigapiforwords.png

Offline

 

#2 2012-05-06 15:55:59

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: HTML5/Javascript problem (again!)

There are three problems I can see:
* You forgot to close the function (with an } at the end)
* You need to use the .value property of the <input> to get its value
* You need to use the comparison operator == instead of the assignment operator = inside the if.

It should probably look something more like this:

Code:

function check() {
    var answer = document.getElementById('AnswerBox').value;
    if (answer == "<h1>"){
        window.location = "scratch.mit.edu";
    }
}

Try installing a Firefox extension such as Firebug so you can see these kinds of errors yourself in the console. (Chrome has something similar.)  smile

I'd also suggest using a library like jQuery that can handle some stuff for you, like being able to select elements using CSS selectors.

Last edited by blob8108 (2012-05-06 15:59:33)


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#3 2012-05-06 16:13:27

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Re: HTML5/Javascript problem (again!)

blob8108 wrote:

There are three problems I can see:
* You forgot to close the function (with an } at the end)
* You need to use the .value property of the <input> to get its value
* You need to use the comparison operator == instead of the assignment operator = inside the if.

It should probably look something more like this:

Code:

function check() {
    var answer = document.getElementById('AnswerBox').value;
    if (answer == "<h1>"){
        window.location = "scratch.mit.edu";
    }
}

Try installing a Firefox extension such as Firebug so you can see these kinds of errors yourself in the console. (Chrome has something similar.)  smile

I'd also suggest using a library like jQuery that can handle some stuff for you, like being able to select elements using CSS selectors.

That's a nice suggestion and all, but really, the only problem with this script is the way he did the redirect.  He used:

Code:

window.location = "scratch.mit.edu";

When he should've used:

Code:

window.location.href = "http://scratch.mit.edu/";

http://i.imgur.com/BAEgGDL.png

Offline

 

#4 2012-05-06 16:15:27

PullJosh
Scratcher
Registered: 2011-08-01
Posts: 500+

Re: HTML5/Javascript problem (again!)

blob8108 wrote:

There are three problems I can see:
* You forgot to close the function (with an } at the end)
* You need to use the .value property of the <input> to get its value
* You need to use the comparison operator == instead of the assignment operator = inside the if.

It should probably look something more like this:

Code:

function check() {
    var answer = document.getElementById('AnswerBox').value;
    if (answer == "<h1>"){
        window.location = "scratch.mit.edu";
    }
}

Try installing a Firefox extension such as Firebug so you can see these kinds of errors yourself in the console. (Chrome has something similar.)  smile

I'd also suggest using a library like jQuery that can handle some stuff for you, like being able to select elements using CSS selectors.

It works! Thanks! I'm going to look into Firebug, too.


http://www.blocks.scratchr.org/API.php?action=text&amp;string=I'm_on_vacation!&amp;xpos=155&amp;ypos=90&amp;font_size=30&amp;bgimage=http://imageshack.us/a/img339/7215/sspeechsigapiforwords.png

Offline

 

#5 2012-05-06 16:42:31

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: HTML5/Javascript problem (again!)

Closed by request of topic owner.


http://i.imgur.com/WBkM2QQ.png

Offline

 

Board footer