Reputation: 1207
My site has a background that looks cool, but might impair usability (and it burns if you look at it too long) so I want a link that changes/removes background image with javascript. I've googled it, but can't find something that works.
Is there a way to make a link like [a href="javascript:???????"]remove bg[/a]?
Upvotes: 2
Views: 341
Reputation: 40555
You could use jQuery CSS function.
$('#removebg').click(function(){
$('body').css("backgroundImage","yoururl");
});
<a id="removebg">removebg</a>
Upvotes: 0
Reputation: 21893
If your background image is NOT specified in the body tag.
<script type="text/javascript">
var img = document.getElementById('your image ID');
var shown = true;
function toggleImg(){
if(shown == true){
img.style.display = "none";
shown = !shown;
}else{
img.style.display = "";
shown = !shown;
}
}
</script>
If your background image is specified in the body tag.
<script type="text/javascript">
var imgSrc = "your image source"
var shown = true;
function toggleImg(){
if(shown == true){
document.body.background = "";
shown = !shown;
}else{
document.body.background = imgSrc;
shown = !shown;
}
}
<a href="javascript:toggleImg();">remove bg</a>
Upvotes: 1
Reputation: 21106
Since you don't tell us how you put in your background image (ie. the html/css markup) it's difficult to give you an accurate answer.
javascript:var f=eval("document.getElementsByTagName('BODY')[0].style.backgroundImage='inherit';");
Upvotes: 0
Reputation: 94499
function removeBackgroundImage()
{
document.body.background = "";
}
<a href="#" onclick="removeBackgroundImage();">Change Background</a>
Upvotes: 1