3
How to use code session document.querySelector
to change/change the background image of a page <body>...</body>
Ex:
var imagem_lista = [
"http://static.gamespot.com/uploads/original/1179/11799911/2324138-warface11.jpg",
"http://glasshome.tv/wp-content/uploads/2013/10/Warface_Screenshot019_Climb.jpg",
"http://hq.warface.com/updates/endless-skies/img/WF_ES_019.jpg",
"http://lodik.ru/uploads/posts/2015-02/1423304948_warface-3.jpg",
"http://hq.warface.com/updates/siberia/img/wf-update-coldpeak-03.jpg",
"http://vignette1.wikia.nocookie.net/warface/images/4/43/Warface_WeapCustom_Combat_201.jpg/revision/latest?cb=20130301001625"
];
window.onload=function(){
var url_pagina = window.location.href;
if(!url_pagina.indexOf('/p/')){
var numero_aleatorio = random_number(0, imagem_lista.lenght);
mudar_imagem(imagem_lista[numero_aleatorio]);
}
}
function random_number(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function mudar_imagem(image){
// usar este
document.querySelector('body').style.background-image=image;
// ou este
$('body').css('background-image', 'url(' + image + ')');
}
And one of the mistakes is this::
Uncaught ReferenceError: Invalid left-hand side in assignment
Uncaught ReferenceError: imagem_lista is not defined
at :2:7
at Object.InjectedScript._evaluateOn (:905:140)
at Object.InjectedScript._evaluateAndWrap (:838:34)
at Object.InjectedScript.evaluate (:694:21)
NOTE: The File containing the script is hosted on:
http://brasilr2.net76.net/js/webmaster.body.js
On the line: indexof
I want to identify when a user accesses my blog to change the background of the image according to a theme set.
Ex: The blog home is set to with one of these images on imagem_lista
.
(http://warfeicebrasil.blogspot.com.br)
Now for example on the survival page of the game quoted there, the theme should soon change the background too. (http://warfeicebrasil.blogspot.com.br/p/operacao-cold-peak.html)
OK your answer was very valid but when I enter the home of the site it still does not change based on the script: brasilr2.net76.net/js/webmaster.body.js
– FRNathan13
In the home you must change the background by any image by the array of the image_list
– FRNathan13
Okay I’m waiting for you...
– FRNathan13
worked if I invoke the change_image method
– FRNathan13
but out of that comes this GET http://warfeicebrasil.blogspot.com.br/undefined 404 (Not Found)
– FRNathan13
if I press F12 and invoke the method happens, but otherwise GET http://warfeicebrasil.blogspot.com.br/undefined 404 (Not Found)
– FRNathan13
this indicating the line 24 of the file that gives the exception: Document.querySelector('body').style.backgroundImage = 'url(' + image + ')';
– FRNathan13
@Nathan1302 I found the problem and updated the answer :)
– Guilherme Nascimento
really now it worked! I always get confused with lenght and length
– FRNathan13
@Nathan1302 I too, I keep getting confused, one that I confused a lot was
keyCode
andkeycode
, in fact theC
is capitalized, but I sometimes forget :) is normal– Guilherme Nascimento
: } obridago! and let me ask in the list ex: has 4 items the length will be 0, 1, 2, 3 not 1, 2, 3, 4. I can apply -1 in the length?
– FRNathan13
@Nathan1302 is right, I forgot to see this detail :)
– Guilherme Nascimento