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

流过昼夜 提交于 2019-12-03 17:07:11

问题


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


回答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.




回答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>
<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.




回答3:


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:

  • 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,



回答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.




回答5:


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



来源:https://stackoverflow.com/questions/10357716/how-do-i-embed-javascript-code-into-a-google-sites-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!