Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » How to run a JavaScript function in a Scratch project
- samq64
-
Scratcher
1000+ posts
How to run a JavaScript function in a Scratch project
How to run a JavaScript function in a Scratch project
For our example JavaScript function we will be creating a dialog box displaying the message “Hello world!”
alert("Hello world!");
Start by opening a new Scratch project and create these cloud variables:
(☁ eval)
(☁ eval output)
(☁ eval error)
The eval cloud variable is used to run the JavaScript function, the eval output cloud variable returns the output of the JavaScript function and the eval error cloud variable displays an error if there is one while running the JavaScript function. All three variables are required and must be be cloud variables and have the same names as above.
After you create your variables, check the boxes next to them to make the monitors appear on the stage.
Now create your simple script to display an alert dialog box:
when green flag clicked
set [☁ eval v] to [alert("Hello world!");]
If you click the green flag, nothing will happen, this is because your three variables are just normal cloud variables. To make it work, you first need to compile the project with Sheep_maker's HTMLifier.
Save the project to your computer and go to https://sheeptester.github.io/htmlifier
For your project source, choose “Upload project file” and select your project.

Change any other options you like, but make sure to keep the box that says “Give certain cloud variables special behaviours depending on the name?” checked.

Click “HTMLify!” and open the file after it's finished downloading.
If everything went well, you should see a dialog box displaying the message “Hello world!” and the variable monitors should look like this:

If not, check the “eval error” monitor to see what went wrong, then go back to your project, fix your errors and recompile it.
Notes
- Since this doesn't work unless the project compiled with HTMlifier, you can't share it on the Scratch website. If you want to share your it with others, you'll need to host your it on your own website. (Or you can upload it onto a game site.) I recommend using GitHub Pages.
- If you would like more information about JavaScript cloud variables and other special cloud variable behaviours using HTMLifier, then you can find more information here.
- I am not an expert at JavaScript, so if you're having trouble with the scripts themselves, please don't ask here.
Other Things To Try
- Displays a dialog with two buttons: OK and Cancel. returns true if OK was clicked and false if cancel was clicked.
confirm("Are you sure?");
- Similar to the ask () and wait block, except it's a dialog box.
prompt("Enter your name:");
- Changes the way the mouse cursor looks. A full list of cursors can be found here.
document.body.style.cursor = "pointer";
Last edited by samq64 (May 3, 2021 00:09:21)
- Vaibhs11
-
Scratcher
1000+ posts
How to run a JavaScript function in a Scratch project
Cool! Can it do browser js/DOM manipulation
- samq64
-
Scratcher
1000+ posts
How to run a JavaScript function in a Scratch project
Cool! Can it do browser js/DOM manipulationWell, it can change the mouse cursor, so yes, I think so.
Last edited by samq64 (May 2, 2021 19:30:09)
- 6d66yh
-
Scratcher
100+ posts
How to run a JavaScript function in a Scratch project
Is it possible to run Intel assembly this way?
- airplanedodge
-
Scratcher
1000+ posts
How to run a JavaScript function in a Scratch project
!Cool! Can it do browser js/DOM manipulationWell, it can change the mouse cursor, so yes, I think so.
there goes tolors thrown straight out the window
- DifferentDance8
-
Scratcher
1000+ posts
How to run a JavaScript function in a Scratch project
Then again, it only works on specific projects made using a specific compiler that's deprecated anyway and has no way of working on the official Scratch website, so I don't think this will affect TOLORS much.!Cool! Can it do browser js/DOM manipulationWell, it can change the mouse cursor, so yes, I think so.
there goes tolors thrown straight out the window
=======
Cool! However, wouldn't this potentially be used to scrape Scratch session cookies and send it to some server and then hack your account? We've already had somewhat of a major problem a while ago where there was a fake tutorial going around on how to “beam” other accounts that (ironically enough) got your own account beamed
- BigNate469
-
Scratcher
1000+ posts
How to run a JavaScript function in a Scratch project
But
works…
Also, please don't post in old and inactive topics- this is known as necroposting.
<!DOCTYPE html> <html> <body> <script> // Insert JavaScript here </script> </body> </html>
Also, please don't post in old and inactive topics- this is known as necroposting.
Last edited by BigNate469 (May 29, 2024 15:11:21)
- badstratch12
-
Scratcher
6 posts
How to run a JavaScript function in a Scratch project
alert ('Hello World')- Discussion Forums
- » Advanced Topics
-
» How to run a JavaScript function in a Scratch project







