views:

43

answers:

2

I'm trying to change an image's imageurl on an aspx asp.net c# page based on a variable from #var=1` (or 2, or 3, or 4)

I know nothing about javascript unfortunately which is what I've been told I need. Can anyone point me at a novice based script I can try to learn via implementation?

+1  A: 

Unfortunately, the answer is going to be as vague as the question.

It seems clear that you want to know how to modify the src attribute of an image.

Given this image in your HTML:

<img id="myImage" src="someOldValue" />

You could use this javascript code to get the image by its ID, and change the src attribute.

<script type="text/javascript">     

    document.getElementById('myImage').src="someNewValue";

</script>

Just place the script at the bottom of your HTML body tag, and it should work.

Please note, that this is a pure javascript solution. If you're going to use jQuery, it may be worth taking a jQuery approach depending upon your actual scenario.

patrick dw
Yeah I think that's what he's looking for, with the addition of using `window.location.hash`. But we're both guessing here. I'll upvote in 2 hours :-)
Josh
sorry for the vagueness, got mentioned 3 times! i'll try to be more detailed in future. i think you were pointing me towards a direction that could work, thank you for that
korben
@korben - Glad you found what you needed.
patrick dw
+2  A: 

Your question is very vague so I'm not sure exactly what you need... I'm just going to write some code and see if it's close to the mark

Try something like

<img id="someUniqueIdYouMakeUp">
<script type="text/javascript">
theImage = document.getElementById("someUniqueIdYouMakeUp");
if(window.location.hash == "#var=1")
{
  theImage.src = "/some/image.jpg";
}
else if(window.location.hash == "#var=2")
{
  theImage.src = "/some/other/image.jpg";
}
</script>

EDIT: Based on your comment, you're looking for the image to update even if the hash changes after the page has loaded. For that, you'll need code similar to the following:

var updateImageWhenHashChanges = function()
{
  theImage = document.getElementById("someUniqueIdYouMakeUp");
  if(window.location.hash == "#var=1")
  {
    theImage.src = "/some/image.jpg";
  }
  else if(window.location.hash == "#var=2")
  {
    theImage.src = "/some/other/image.jpg";
  }
  // Tell the window to call updateImageWhenHashChanges() again in 500 miliseconds:
  window.setTimeout(updateImageWhenHashChanges,500);
}
updateImageWhenHashChanges();

For more information, read about window.setTimeout.

Josh
exactly what i was looking for, thanks. sorry for the vagueness, got mentioned 3 times! i'll try to be more detailed in future
korben
+1 Since you realized his hash requirement.
patrick dw
@korben: No problem, I'm glad I could help.
Josh
@patrick: The `#var=1` was the only clue :-) I do think jQuery would be a benefit; your answer was more thorough and I'm just waiting for SO-time to roll over so I can get more votes :-)
Josh
@Josh - Is there a daily max, or something?
patrick dw
@patrick: [30 question/answer votes per day](http://meta.stackoverflow.com/questions/28314/why-are-there-voting-limits) and I seem to have reached that around 11AM today, LOL
Josh
@patrick Much better explanation: [Are there any voting limits?](http://meta.stackoverflow.com/questions/5212/are-there-any-voting-limits)
Josh
@Josh - Just (finally) read the faq. A little overdue I guess.
patrick dw
not sure if you're still checking this but thought i'd ask. i'm not getting the effect i had hoped for. as implemented i can add the #var=2 and see no change, then if i reload that page, the expected image shows up. am i supposed to preload the images in javascript, hidden, or something like that to get this to work so that a user clicks and instantly sees the other image?
korben
@korben: OK. That's more in-depth. You'll need to periodically check for changes in `window.location.hash`... I'll update my answer in a moment.
Josh
i'm going to try this! thank you! if you don't mind i will get back to you in a bit with how my progress is
korben
what if i wanted to associate the update with a click on a linkbutton instead of on a timed interval? just onclientclick="updateImageWhenHashChanges()" ??
korben
<a href="#size=3" onclick="updateImageWhenHashChanges();">test</a> i tried that, it works on the SECOND click, as if the href is happening after the update function, so it does nothing the first time
korben
@korben: Try: `<a href="#size=3" onclick="window.location.hash='#size=3';updateImageWhenHashChanges();">test</a>`
Josh