Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including javascript to google sites

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?

like image 555
VidalDuval Avatar asked Feb 08 '13 20:02

VidalDuval


People also ask

Does Google Sites allow custom JavaScript?

Google sites now allow one to embed Javascript snippets into a site; thereby providing some interesting new capabilities with websites built with Google sites.

Can you use code with Google Sites?

You can embed CSS, HTML, or JavaScript code directly into your Site. Under the Insert tab to the right, select Embed. Next, select the Embed code tab and paste the code into the textbox. Finally, click Next and then click Insert.

How do you add plugins to Google Sites?

Go to the Sites page that will contain the new gadget. Open the page for editing. Select Insert > More gadgets. Search for the gadget, select it from the categories on the left, or click Add gadget by URL and paste in the URL to your .

Can you use custom HTML in Google Sites?

Add an HTML Box to classic Google Sites Navigate to the page where you want to add the HTML, CSS or JavaScript. Use the Insert menu, then HTML Box. Once the Insert HTML Box editor appears add the code you want to use.


2 Answers

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 ;-)

like image 149
Marvin Rabe Avatar answered Oct 21 '22 03:10

Marvin Rabe


Google Sites will filter out operations it considers unsafe. Some details here, but was unable to find official documentation with quick search.

As another answer mentions, your variables need var declarations. This is required because without this, the variables will become global to the window, and potentially could be used to escape whatever sandbox Google is putting around the sites javascript support, potentially causing security concerns.

So to fix:

var x = parseFloat(document.getElementById("value1").value);
var y = parseFloat(document.getElementById("value2").value);
like image 1
Ben McCormick Avatar answered Oct 21 '22 02:10

Ben McCormick