# onProjectOpen Event

Hello,

I have a Snap! instance running locally in an iFrame at a URL that is whitelisted for authentication. Is there a way to subscribe to an event that is fired when a user opens a Snap! project from the cloud? If so, how would I subscribe to such an event and access the URL of the Snap! project that was opened? Thanks.

If you add a runFlag flag to the url, you will get green flag on start. You can also use the when block with not a transient variable, and set it to true in the script.

Hmm, at one time I wanted to be able to have a STARTUP block in a project that would auto-run when the project is loaded, but Jens vetoed that because he wanted to avoid people writing projects that delete all your projects, or something, before you even know they're running. But I guess we can achieve the same effect this other way. @jens, does this worry you? Or is it okay?

1 Like

You could make it so it starts as if the stop button was pressed. You could also disable JS calling (don't disable the function blocks) until a flag or key is pressed. You would have to be make sure to not allow it just because the project broadcasts __shout__go__.
I can already make a link that runs on load by removing the noRun flag. It's even the default!

Thanks for the help; that should cover the, "on load," part.

Is there a way to access the URL of the currently-loaded cloud Snap! project? If so, how would I do that?

There is no way to do this without JS. window.location contains a lot of data, including the hash and the href. It also contains the protocol, the host, the port, and the pathname.

@spaceelephant I have access to JS, but I am using an iFrame. Thus, when I try accessing iFrame.contentDocument.location.href, the value I receive is not accurate. This is because, even after loading a project from my Snap! account, the href points to the original domain of the iFrame. This may be related to the cross-domain issue iFrames have (https://stackoverflow.com/questions/44359/how-do-i-get-the-current-location-of-an-iframe#:~:text=location.,user%20navigates%20in%20the%20iframe.). Any ideas on how to get around this?

Can you put the javascript block in the project? I hav'nt really used iframes. I wish they still had normal frames. (they wouldn't help in this case however)

@spaceelephant Yeah, but, unfortunately, window.location from within the Snap! project, just like window.location from within the iFrame itself on my website, does not contain the URL of the Snap! project from the cloud, which is what I am looking for. Any other ideas?

Hi Eric, can't you just look up document.location ? Since you'll be inside the iFrame anyway it'll give you all you need, or what am I missing here?

Here's a quick test: https://snap.berkeley.edu/project?user=jens&project=iFrame%20URL%20Sniffer

1 Like

@jens
That approach works on Snap!:

However, unfortunately, it doesn't provide me with the information I am looking for on my website, from which I am running Snap!:

I believe the confusion is coming from the fact that I am not using solely an iFrame; I am using Snap!'s source code, hosted on my website, within an iFrame, from which users can log in and open their Snap! cloud projects. Any other ideas for catching the Snap! cloud URL of projects opened on my site would be greatly appreciated. Thanks for the help so far!

In the snap project, run window.parent.loadHash(window.location.hash);.
In your website, define a function window.loadHash that will be run to get the hash.

Your Snap! copy can be modified to broadcast window.postMessage() with project URL.

1 Like