Google Sites – Using JavaScript

google-sites

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.

Best Answer

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.