Can use document.createElement
with appendChild
and insertBefore
, to insert do this can do so:
//Esta função é mais rapida que usar window.onload
function readyDom(callback) {
if (/^(interactive|complete)$/i.test(document.readyState)) {
callback();
} else {
document.addEventListener("DOMContentLoaded", callback);
}
}
//vai inserir dentro do head antes do </head>
readyDom(function() {
var navbar = document.createElement("link");
navbar.rel = "stylesheet";
navbar.type = "text/css";
navbar.href = "http://cdn.grupospotlight.com/nav/navbar.css";
document.head.appendChild(navbar);
});
//vai no <body>, antes de qualquer elemento
readyDom(function() {
var ulnav = document.createElement("ul");
ulnav.className = "spot-nav";
ulnav.innerHTML = '<li class="left spot-logo">' +
'<a class="spot-a"><div class="spot-logo"></div></a>' +
'</li>' +
'<li class="right">' +
'<a class="spot-a" href="http://www.grupospotlight.com/" target="_blank">WWW.GRUPOSPOTLIGHT.COM</a>' +
'</li>';
document.body.insertBefore(ulnav, document.body.firstChild);
});
If you need a delay use setTimeout
:
//vai no <body>, antes de qualquer elemento
readyDom(function() {
var segundos = 3; //3 segundos
setTimeout(function() {
var ulnav = document.createElement("ul");
ulnav.className = "spot-nav";
ulnav.innerHTML = '<li class="left spot-logo">' +
'<a class="spot-a"><div class="spot-logo"></div></a>' +
'</li>' +
'<li class="right">' +
'<a class="spot-a" href="http://www.grupospotlight.com/" target="_blank">WWW.GRUPOSPOTLIGHT.COM</a>' +
'</li>';
document.body.insertBefore(ulnav, document.body.firstChild);
}, 1000 * segundos);
});
I recommend you study on DOM:
What is the need to add with a Javascript and not directly in the file? It will be loaded dynamically after a user action?
– Giancarlo Abel Giulian
⇨ http://answall.com/questions/97176/como-carregar-javascript-corretamente-de-forma-din%C3%A2mica
– Daniel Omine
http://answall.com/questions/114647/load-javascript-de-uma-url-atrav%C3%A9s-da-barra-endere%C3%A7os-ou-bookmarklet
– Daniel Omine