Wrox Programmer Forums
|
HTML Code Clinic Do you have some HTML code you'd like to share and get suggestions from others for tweaking or improving it? This discussion is the place.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the HTML Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 29th, 2004, 04:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default the background tag

Hi there

I'm wanting to use a variable image (using javascript) on my page; however, I want to make it a variable background image using the background tag - I don't seem to be able to attach a name attribute to define the background so that the script picks it up.

 <td width="874" height="113" valign="top" background="image_bin/spacer.gif">

I need a name attribute on this i.e name="pic" but it's not liking it.

Is there any way I can do this?

thanks

Adam
 
Old April 29th, 2004, 08:16 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Use an ID if you want the script to be able to pick it up, then reference it by document.getElementById('your_ID');

HTH,

Snib

<><
 
Old April 29th, 2004, 08:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

Hi Snib

I've tried what you mentioned but it doesnt'want to work; this is my coding:

myPix = new Array("image_bin/lpe_top_banner.jpg","image_bin/topbanner2.jpg","image_bin/topbanner3.jpg","image_bin/topbanner4.jpg")
    imgCt = myPix.length

    function choosePic() {
        if (document.images) {
            randomNum = Math.floor((Math.random() * imgCt))
            document.pic.src = myPix[randomNum]

                document.getElementById('pic');

        }
    }

</script>

      <td width="874" height="113" valign="top" background="image_bin/spacer.gif" id="pic">

thanks

Adam
 
Old April 29th, 2004, 08:58 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I really didn't explain myself....

Try this for function choosePic():

Code:
myPix = new Array("image_bin/lpe_top_banner.jpg","image_bin/topbanner2.jpg","image_bin/topbanner3.jpg","image_bin/topbanner4.jpg");
var numPix = myPix.length;

function choosePic()
{
 var picture = document.getElementById('pic');
 var rand = Math.floor(Math.random() * numPix);
 picture.src = myPix[rand];
}
Make sure your image has id='pic' as an attribute.

HTH,

Snib

<><
 
Old April 29th, 2004, 09:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

thanks Snib, but it still don't seem to work:

http://80.82.139.249/st-sar/header2.asp

any further suggestions?

Adam
 
Old April 30th, 2004, 09:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 249
Thanks: 0
Thanked 0 Times in 0 Posts
Default

if you did it in asp you could use this
<%

Dim bgImage(6)
Dim intbgImage_01

bgImage(0) = "bgImage_0.jpg"
bgImage(1) = "bgImage_1.jpg"
bgImage(2) = "bgImage_2.jpg"
bgImage(3) = "bgImage_3.jpg"
bgImage(4) = "bgImage_4.jpg"
bgImage(5) = "bgImage_5.jpg"

RANDOMIZE
intbgImage_01 = Int(6 * Rnd)

Response.Write bgImage(intbgImage_01) & "---image Name<BR>"

%>
 
Old April 30th, 2004, 09:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

might be an idea, thanks alot
 
Old May 3rd, 2004, 03:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Adam,

Using ASP is one way to do it, but there is a client side / JavaScript / DHTML solution as well. Instead of trying to get at the image directly, try to get at its parent. The background image is really a property of the table cell you're trying to change. So, you can use JavaScript to get a reference to the <td> element, and then use its CSS style.backgroundImage (note that CSS uses background-image) to change the image. Below you'll find a working example; copy it to a new document and then just click the button to see the image change:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Change Background</title>
    <script type="text/javascript">
        function ChangeBackground()
        {
            document.getElementById('myTableCell').style.backgroundImage = 
                    'URL(/Images/Image2.gif)';
        }
    </script>
</head>
<body>
<table width="200" border="0">
<tr>
    <td id="myTableCell" background="/Images/Image1.gif">
        I am a cell with a background image<br />
        <br />
        If you click the button, my background will change.
    </td>
</tr>
</table>
<form id="frmTest" name="frmTest" action="" method="get">
    <input type="button" id="btnDo" 
        name="btnDo" onclick="ChangeBackground()" 
        value="Click to change Backgound" />
</form>
</body>
</html>
If you go this route, you should also change the deprecated background property of the <td> element to newer css style="background-image=..." syntax.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Dissolved Girl by Massive Attack (Track 6 from the album: Mezzanine) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML tag from C# or ASP.NET tag from javascript angshujit ASP.NET 2.0 Basics 3 February 16th, 2007 10:07 AM
Background Image problem in DIV tag. cumminsj CSS Cascading Style Sheets 6 September 15th, 2006 03:02 AM
HTML tag vs Body Tag CFGerry BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 October 7th, 2005 07:13 AM
Trying to output a tag within a tag jaucourt XSLT 3 January 12th, 2005 11:57 AM
the background tag Adam H-W Dreamweaver (all versions) 1 May 3rd, 2004 03:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.