avinashbot
avinashbot

Reputation: 1207

Javascript BG Image Change

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

Answers (4)

Blowsie
Blowsie

Reputation: 40555

You could use jQuery CSS function.

$('#removebg').click(function(){
     $('body').css("backgroundImage","yoururl");
});

<a id="removebg">removebg</a>

http://api.jquery.com/css/

Upvotes: 0

124697
124697

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

Louis Ricci
Louis Ricci

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

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

function removeBackgroundImage()
{
       document.body.background = "";
}

<a href="#" onclick="removeBackgroundImage();">Change Background</a>

Upvotes: 1

Related Questions