I need to use a JavaScript patch in my Google Sites, but the problem is I don't understand how to place them and use it as Google Gadget.

<!DOCTYPE <strong class="highlight">html</strong> PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<<strong class="highlight">html</strong> xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
var timerid = 0;
var <strong class="highlight">images</strong> = new Array(  "image1.jpg",
            "image2.jpg",
            "image3.jpg");
var countimages = 0;
function startTime()
{
    if(timerid)
    {
        timerid = 0;
    }
    var tDate = new Date();

    if(countimages == <strong class="highlight">images</strong>.length)
    {
        countimages = 0;
    }
    if(tDate.getSeconds() % 5 == 0)
    {
        document.getElementById("img1").src = <strong class="highlight">images</strong>[countimages];
    }
    countimages++;

    timerid = setTimeout("startTime()", 1000);
}
</script>
</head>

<body onload="startTime();">
<img id="img1" src="image3.jpg" />
</body>
</html>

I'm hoping for specific instructions using the JavaScript code I provided since I've found general Google Sites embedding instructions unfathomable. However with specific instructions I will be able to figure out how to use scripts in Google Sites in the future as well.

up vote 4 down vote accepted

You cannot add JavaScript directly to your Google Sites page. You can, however add a Google Gadget that has JavaScript in it. You can try the Google Gadget Builder. You may not get the result that you want, however, since your JavaScript is not valid. For example, your statements with (x)html elements will cause the JS engine to error.

var <strong class="highlight">images</strong> = new Array(  "image1.jpg",
        "image2.jpg",
        "image3.jpg");

It may work with E4X, but Google Sites is not rendered as XML. (Yes, it does have the XML doctype, but not the header, so it will not render as XML)

In addition to Google Gadgets, you can also try Google Apps Scripts. It allows for some server side automation.

I am not sure what you are trying to do with your script, but have you looked too see if you can accomplish what you want with a Google Apps Script? It looks like using Google App Scripts within Google Sites is pretty easy. Here is a tutorial - Getting Started with Apps Script and Google Sites.

You can also use CodeWrapper. It's a gadget that allows you to paste your javascript into it and run it as a google gadget, basically allowing you to enter the javascript into the html without it being blocked. Go to Insert > more gadgets > search for codewrapper.

It should solve your problem. It's solved a few of mine already.

  • 1
    How do you use it? – Oxwivi Sep 23 '11 at 11:04

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.