I have a Google Sites page. How can I embed JavaScript code in it, without implementing a gadget and adding JavaScript code in that?
You can use the HTML Box
gadget, here is where you find it in the menu:
It allows you to Insert custom HTML, CSS, and Javascript.
Caveats:
<iframe>
,HTML Box
HTML Box
does not currently support <iframe>
,<script>
, <img>
or <a>
tags,Refer to this article you need to create a Google Gadget file that you can access from a server to share the file and write the following code:
<Module>
<ModulePrefs title="Custom Gadget"/>
<Content type="html">
<![CDATA[
<script src="PUT YOUR SCRIPT HERE"></script>
]]>
</Content>
</Module>
Save the file as Name.xml and upload it to a file hosting service that has public access (not password protected). Then insert the link as following:
Insert > .. More Gadget > Add gadget by URL
Once you copy your url to the saved .xml Google Gadget code, you will have javascript embedded through your very own custom Google Gadget.
You can run some JavaScript code, but during page editing it will be automatically wrapped into a trivial gadget, and when rendering page it will be executed inside of iframe, and won't be able to interact with DOM of main page - this is prevented by security mechanisms of browser and Google Sites.
So, anything your script do, it will do in a sandbox of it's iframe, who's outer size is defined at page design time, if you don't want to implement a gadget.
And even if you implement a gadget with your script inside, it will also run in the same iframe sandbox. But gadget has some advantages, for example access to API, that allows to change size of iframe dynamically, and some others.
Google has updated their mechanism for embedding HTML and Javascript.
See https://gsuiteupdates.googleblog.com/2017/12/embed-html-and-javascript-in-new-google-sites.html.
It is still sandboxed in an iframe with the limitations mentioned in @Serge Populov's answer.
You can read Googles documentation on how to do this here http://support.google.com/sites/bin/answer.py?hl=en&answer=2500646&ctx=cb&src=cb&cbid=-11zqfs2rw8yfe&cbrank=0