1

I am using the following code :

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication3.Site1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="style.css" rel="stylesheet" />
    <script type="text/javascript">

        function change(color) {
            document.bgColor = color;
        }

    </script>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">

        <p>&nbsp;</p>
        <div class="green">
            <div id="slatenav">
                <ul>
                    <li><a href="WebForm3.aspx" id="green" onclick="change('green')">Home</a></li>
                    <li><a href="WebForm4.aspx"  id="red" onclick="change('red')">About Us</a></li>
                </ul>
            </div>
        </div>
        <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
    </form>
</body>
</html>

Why it isn't working properly?

2 Answers 2

3

While you shouldn't be using document.bgColor, it should still work. The problem isn't that -- but rather that you're using it with links which are cancelling the javascript (the link is loading a new page).

Give this a try:

<script type="text/javascript">
     function change(color) {
     document.bgColor = color;
     return false;
     }
</script>
<div id="slatenav">
    <ul>
        <li><a href="WebForm3.aspx" id="green" onclick="return change('green');">Home</a></li>
        <li><a href="WebForm4.aspx"  id="red" onclick="return change('red');">About Us</a></li>
    </ul>
</div>

And the sample fiddle

It won't load the new page, but the Javascript will fire off. And as others have suggested, use document.body.style.backgroundColor.

Sign up to request clarification or add additional context in comments.

1 Comment

@user1799345 -- np, glad we could help!
1

The document.bgColor is deprecated use document.body.style.backgroundColor instead

From https://developer.mozilla.org/en/docs/DOM/document.bgColor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.