tags:

views:

58

answers:

5

So, using IE as my browser, I have a simple link...

<a href="test.jpg">Image Link</a>

Is it possible to change the background color of the page this image loads up in?

I want to avoid creating a whole new page for the image.

EDIT Sorry for the confusion. I thought it was pretty clear. Pascal has it right as does Guffa.

from Pascal... "I understood that he want the image opened in a new page, and that page to have a specific background-color"

@David Andes, you are correct in that the link implies what is happening ("I know the image is part of the anchor itself"), which is that the image will be opened in a new page. However, the language you quoted is not correct.

"change the background of the image this page loads up in."

Ultimately, I would like to change the background color of the page that this image is opened in.

So the answer to the comment on this post is indeed (1) want the background color to change for the page loaded by a click to "Image Link,"

A: 

That's not possible. As an alternative, you can use one of the many javascript-based image viewers, like Lightbox 2

Philippe Leybaert
A: 

No, that's not possible. If you want to affect anything about how the image is shown (background color, margin, et.c.) you have to create a page for it.

If you have many images to show this way, you don't need to create one page for each. You can use a single page and send the image to show as query string.

Guffa
+2  A: 

When someone clicks on such a link, the only thing the browser loads up is the image itself -- not any "page".

This means the background-color depends on the browser, and there is nothing you can do about it.

If you want your image to be displayed in any other "page", you'll have to code that one -- still, if you have several images, you don't need to code a different page for each one : a generic page, able to display any image, and received a parameter indicating which image should be displayed, will do the trick.


Just for fun : under firefox, that background-color can be changed by going to about:config, searching for the browser.display.background_color property, and changing it.

Of course, as a webmaster, you have no control over it : only a use can change it, for his instance of firefox.

Pascal MARTIN
+2 because all these other bakkas keep suggesting lightbox which is pretty far off from what the OP had asked.
Mark
@Pascal: I must be completely misunderstanding the OP's question. Does he not want to alter the document's background color based on whether or not one of its anchor tags is pointing to a specific location?
David Andres
@David : I understood that he want the image opened in a new page, and that page to have a specific background-color ;; but maybe I didn't understand well, actually ?
Pascal MARTIN
@Pascal: I'm leaning on the expression "change the background of the image this page loads up in." I know the image is part of the anchor itself, but the language seems to imply that he wants to alter the background color of the image's parent page itself.
David Andres
@David : I'm guessing we'll know (I hope so ^^ ) in some time, when the OP comes back on this page ;-)
Pascal MARTIN
@Pascal: looks like you were right after all, if the big check mark next to this post is any indication
David Andres
I added an edit for clarification.
Carter
A: 

It's not possible to do this in straight HTML.

How most people solve this now days is to use a javascript media viewer such as Shadowbox to achieve the same effect (cause no one wants to make a page for every image).

T. Stone
A: 

I'm not entirely sure what your end goal is, but you can affect the background color of the browser document (not the window) based on href settings using code similar to the following:

<script type="text/javascript">
 function setBackgroundColorIfAppropriate(anticipatedURL, colorToUse)
 {
   var allAnchorTags = document.getElementsByTagName("a");

   for(var i = 0; i < allAnchorTags.length; ++i)
   {
     if(allAnchorTags[i].href === anticipatedURL)
     {
       document.body.style.backgroundColor = colorToUse;
       break;
     }
   }
 }

 window.onload = function()
 {
   setBackgroundColorIfAppropriate("test.jpg", "blue");
 };
</script>

The href attribute may not exactly match what you've specified in markup, so be careful and alert the contents of the anchor elements' href attributes to see if they match expectations.

David Andres