JavaScript

JavaScript is a client-side web programming language. It makes web pages dynamic in many different ways and it lets programmers and website designers control every element in a web page.

I used JavaScript many times. As an example, I paste a code snippet below. That time I needed to change some text on interface elements (e.g. captions on buttons). I was allowed to insert more Html (and javascript code) into the target web pages, but I didn’t have direct control over the way the rest of the content was generated. So, with this code, after the web page was loaded in the browser the javaScript code executed. It lloked for each element needing translation, then it set its properties – the text caption – to the desired values (a text in italian language). The page istantly changes, and the user sees the new text.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
try{
var elementi = document.getElementsByTagName("input");
for (i=0; i<elementi.length; i++)
if(elementi[i].value=="Add To Cart")
elementi[i].value = "Aggiungi il prodotto";
/*TRADUCE TESTI - quantità prezzo*/
var elementi = document.getElementsByTagName("legend");
for (i=0; i<elementi.length; i++)
if(elementi[i].textContent=="Quantity")
elementi[i].textContent = "Numero";
var priceText = document.getElementsByClassName("pricedisplay wpsc-product-price");
for (i=0; i<priceText.length; i++)
priceText[i].textContent = priceText[i].textContent.replace(/Price/g, "Prezzo");
}catch(e){}

More code:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
try{
/*TRADUCE PULSANTI*/
document.getElementsByClassName("wpsc_buy_button")[0].value = "Acquista";
var elementi = document.getElementsByTagName("input");
for (i=0; i<elementi.length; i++){
if(elementi[i].value=="Update")
elementi[i].value = "aggiorna";
if(elementi[i].value=="Remove")
elementi[i].value = "Elimina";
}
console.log(1);
/*ELIMINA testo - traduce etichette*/
var etichetta = document.getElementsByTagName("label");
console.log(2);
for (i=0; i<etichetta.length; i++){
console.log(3);
var str = etichetta[i].innerHTML;
console.log(4);
str = str.replace("Same as billing address:", "");
console.log(5);
str = str.replace("Name", "Nome");
str = str.replace("Surname", "Cognome");
str = str.replace("Address", "Indirizzo");
str = str.replace("City", "Città");
str = str.replace("Province", "Provincia");
str = str.replace("Country", "Paese");
str = str.replace("Postal code", "Cap");
str = str.replace("Phone", "Telefono");
etichetta[i].innerHTML = str;
}
/*TRADUCE TESTI - prezzo, spedizione, email*/
document.getElementsByClassName("wpsc_totals")[0].textContent = " Spese di spedizione: ";
document.getElementsByClassName("wpsc_totals")[2].textContent = " Costo totale: ";
document.getElementsByClassName("wpsc_email_address")[1].textContent = " Indirizzo e-mail ";
/*TRADUCE TESTI - quantità ecc.*/
var th=document.getElementsByTagName("TH");
for (i=0; i<th.length; i++){ if(th[i].textContent=="Product")
th[i].textContent = "Prodotto";
if(th[i].textContent=="Quantity")
th[i].textContent = "Quantità";
if(th[i].textContent=="Price")
th[i].textContent = "Prezzo";
if(th[i].textContent=="Total")
th[i].textContent = "Totale";
}
var tagH4 = document.getElementsByTagName("h4");
for (i=0; i<tagH4.length; i++){
tagH4[i].innerHTML = tagH4[i].innerHTML.replace("Shipping address (if different from billing address)", "Indirizzo di spedizione (se diverso da quello di fatturazione)");
tagH4[i].innerHTML = tagH4[i].innerHTML.replace("Billing address", "Indirizzo di fatturazione");
}
/*CODICE FISCALE*/
var focused=0;
document.getElementById("wpsc_checkout_form_19").onfocus=function () {
focused=1;
}
document.getElementById("wpsc_checkout_form_19").onblur=function () {
var pattern = /^[a-zA-Z]{6}[0-9]{2}[a-zA-Z][0-9]{2}[a-zA-Z][0-9]{3}[a-zA-Z]$/;
var campo = document.getElementById("wpsc_checkout_form_19");
if(focused)
if (campo.value.search(pattern) == -1) {
alert("Il codice fiscale inserito non è corretto");
campo.value = "";
campo.focus();
}
}
} catch(e){
document.getElementById("istruzioni").innerHTML = "";
} finally{
/*TRADUZIONI*/
var y = document.getElementsByTagName("a");
for(i=0; i<y.length;i++)
if(y[i].textContent=="Please visit our shop")
y[i].textContent="Visita il NEGOZIO";
var elementi = document.getElementsByTagName("p");
for (i=0; i<elementi.length; i++)
elementi[i].innerHTML = elementi[i].innerHTML.replace(/Oops, there is nothing in your cart./,"Attenzione, non sono stati effettuati acquisti. Il carrello è vuoto. ");
}