Hey guys
I am coding a GM script, and one thing I realised that I'm doing repeatedly is doing the same code over and over again. Specifically, the style property.
function createButton() {
var a = document.createElement('a');
a.href = '#';
a.innerHTML = 'Print Topic';
a.style.position = 'absolute';
a.style.right = '3em';
a.style.top = '6em';
a.style.fontFamily = 'Arial,Helvetica,sans-serif';
a.style.fontWeight = 'bold';
a.style.fontSize = '125%';
a.style.background = '#777777 none repeat scroll 0 0';
a.style.color = 'white';
a.style.padding = '6px 12px';
document.body.insertBefore(a, document.body.lastChild);
}
As you can see in my sample code, I repeatedly wrote a.style a lot of times. Do you have techniques that you use to avoid this mess? Just for the sake of gracefulness.
THANKS --
Guys, here's the reduced code:
function createButton() {
var a = document.createElement('a');
var css = document.createElement('style');
css.type = 'text/css';
css.innerHTML = '#prt { position:absolute; right:3em; top: 6em; font-family: Arial,Helvetica,sans-serif; font-weight:bold; font-size:125%; background: #777777 none repeat scroll 0 0; color: white; padding: 6px 12px;}'
a.href = '#';
a.innerHTML = 'Print Topic';
a.id = 'prt';
document.body.insertBefore(a, document.body.lastChild);
document.body.appendChild(css);
}
LOL, that certainly looks better