How do I embed JavaScript code into a Google Sites page?

前端 未结 5 1372
栀梦
栀梦 2021-02-08 04:26

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

相关标签:
5条回答
  • 2021-02-08 04:55

    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,
    0 讨论(0)
  • 2021-02-08 04:57

    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.

    0 讨论(0)
  • 2021-02-08 05:06

    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.

    0 讨论(0)
  • 2021-02-08 05:08

    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.

    0 讨论(0)
  • 2021-02-08 05:09

    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

    0 讨论(0)
提交回复
热议问题