Including javascript to google sites

VidalDuval picture VidalDuval · Feb 8, 2013 · Viewed 9.1k times · Source

I'm trying to include a simple javascript to Google Sites but I get nothing when pressing the button. I put the code inside an HTML Box. The code works perfectly when tested locally. Here is my code:

<script>
  function calcul(){
    x = parseFloat(document.getElementById("value1").value);
    y = parseFloat(document.getElementById("value2").value);
    document.getElementById("answer").innerHTML=x+y;
  }
</script>

<form action="" id="nothing">
  <input type="text" id="value1">
  <input type="text" id="value2">
<input type="button" value="Calculate" id="but" onclick="calcul()" />

<p id="answer"></p>

Is there something I forgot to make it work?

Answer

Marvin Rabe picture Marvin Rabe · Feb 8, 2013

Google Sites changes your entire script. You have to be a bit more careful writing JavaScript.

Adding var in front of every variable will fix your problem:

<script>
  function calcul(){
    var x = parseFloat(document.getElementById("value1").value);
    var y = parseFloat(document.getElementById("value2").value);
    document.getElementById("answer").innerHTML=x+y;
  }
</script>

<form action="" id="nothing">
  <input type="text" id="value1">
  <input type="text" id="value2">
  <input type="button" value="Calculate" id="but" onclick="calcul()" />
</form>

<p id="answer"></p>

This will work ;-)