Call block from Javascript?

I have a JS block that runs as a continuation.
If it possible for the JS to "call" a Snap block via the Javascript?

invoke() is one way to do it. Pass a ringed command, reporter, or predicate to the JS function, then pass that to the invoke() method. Idk rn other ways to do it.

Thanks, but I must be missing something.
I created a block called 'spin'.
Within a Run block, I have a JS block with this code in it:
invoke("spin")

When I click on the Run block, I get this error:
'Expecting a block or ring, but getting spin'
I've tried 'ringing' spin, but that doesn't seem to help

Here is a link to the project:
https://snap.berkeley.edu/snap/snap.html#present:Username=bferster&ProjectName=test2

I can't access the project. Have you shared and published it yet?

Sorru. I'm new to Snap.
It should be published/shared now.

Welcome to Snap! Are you a kid trying to use Snap! to learn JavaScript? That might be a cute idea but we're not yet supporting this with any materials. I would suggest you learn JavaScript the traditional way using ... (Google and StackOverflow) online tutorials. Personally I like the ones from Mozilla best: https://developer.mozilla.org/en-US/docs/Web/JavaScript. Are you a professional programmer interested in writing your own blocks? The Snap! source code is all out there on Github: https://github.com/jmoenig/Snap for you to knock yourself out.

What Snap! offers is a way to learn CS. If you're interested in learning about things like functions, data, recursion, first-class constructs, inheritance, media etc. you'll find Snap! to be a rich system that lets you explore and author interesting things without having to use a textual programming language. In fact, trying to mess with JavaScript is almost certainly going to be a detrimental experience and absolutely won't give you any new or relevant insights.

I'm pretty versed in JS. Definitely not a kid :slight_smile:
een doing it for 10+ years.

Ah, haha, sorry! I saw this in your project:

and figured you couldn't tell a formal parameter from a literal string :slight_smile:

What are you trying to accomplish? What's the point in trying to capture a continuation in JavaScript? What do you want to use it for?

no worries. JS I know. It's Snap I'm new to.

I'm a research professor at UVA trying to use Snap within a learning system we've developed called SCALE. (Student-Centered Adaptive Learning Environment) to teach middle-schoolers CS.

We want to be able to pass messages back and froth from instructional pages to Snap to do things or have our system respond to things from Snap. An instance of snap within a SCALE iFrame.

The idea is to set up a listener in Snap to react to messages sent to it via this method:

window.addEventListener("message",function(e) { if (e.data.match(/scaleact/i)) alert("Do something interesting") },false);

Ah, now we're talkin' :smiley:.
Wow, Scale sounds (and looks) like an amazing and very interesting project.
I guess it's fairly easy for Snap! to trigger things in Scale, because that basically involves sending textual messages. The other way round is a little more interesting, and - frankly - I don't know myself how to do it. I guess I now understand your question about continuations, because those would let you "goto" or jump to a specific part of a program to resume Snap. Problem with this approach is that continuations are basically non-serializable (because in order to serialize then you'd have to also serialize the whole environment they're in). So, what I would try instead is using textual messages both ways. It's fairly easy to programmatically trigger all hat blocks that respond to a defined (text-symbol) message in Snap. Would that be something that would help you? I guess you could even programmatically insert a script with such an event hat block ("listener") into Snap from Scale, and that would let you start arbitrary processes.

Of course, in the mid-term run we want to add macros to Snap and that will solve all these issues elegantly. In the meantime I have to correct myself, and JS is all we have. Haha!

The iframe remains active the whole time, so each message would be additive.what I want to do is install the listener, which works well in snap, and then trigger snap object from there.

That definitely sounds feasible. Would you work with a Snap! project that already has certain hooks in it or should this be a generic way in which another web page can interact with an arbitrary Snap! project?

It would call a preset snap project that contained hooks to listen to the commands sent. Probably a fixed set of options, but always extensible

Aside from triggering scripts in Snap, would you also need to pass along data? Probably, right? And if so, which kind of data would that be? Just atomic data or lists (of lists of) atomic data (numbers, text, booleans), or also more complex stuff like sprites, costumes, sounds, scripts?

Here's a page I'm playing with:


on the 3rd page,it calls up a snap project

If you click on the "click me" button, it sends a message to the listener in snap to call up an Alert() box.

If you click on the lowe JS block, it will move SCALE to a new page

The persistent communication works well. I just don't know how to get it to call a Snap block.

Okay, now all makes sense to me :smiley:

Lemme try something real quick, I think I know an easier way...

Appreciate it!

It's getting to be bed time over here in Germany, so "real quick" might turn out to be tomorrow morning for me...