Skip to content Skip to sidebar Skip to footer

How Do I Embed Javascript Code Into A Google Sites Page?

I have a Google Sites page. How can I embed JavaScript code in it, without implementing a gadget and adding JavaScript code in that?

Solution 1:

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.

Solution 2:

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><ModulePrefstitle="Custom Gadget"/><Contenttype="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

enter image description here Once you copy your url to the saved .xml Google Gadget code, you will have javascript embedded through your very own custom Google Gadget.

Solution 3:

You can use the HTML Box gadget, here is where you find it in the menu:

selecting HTML Box in the menu.

It allows you to Insert custom HTML, CSS, and Javascript.

Caveats:

  • your HTML is loaded in an <iframe>,
  • your code cannot interact with content outside of the HTML Box
  • the HTML Box does not currently support <iframe>,
  • your script cannot create any <script>, <img> or <a> tags,

Solution 4:

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.

Solution 5:

Post a Comment for "How Do I Embed Javascript Code Into A Google Sites Page?"