473,486 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with Setting the "src" of an "img" using javascript


Hello,

In my asp.net 2.0 page, I have a hyperlink with an arrow image

<a id="imgtest" href="#"onclick="return hideColumn()">
<img id="imgArrow" src="App_Themes/Trmis/WebResource3.gif"
alt="Click" />
</a>

WebResource3.gif is an arrow pointing left and when I click on it I
want to replace it with WebResource4.gif which is an arrow pointing
right, but for some reason the code below is not working.

<script language="javascript" type="text/javascript" >
var testVar = 1;
var aDOM = 0, ieDOM = 0, nsDOM = 0; var stdDOM =
document.getElementById;
if (stdDOM) aDOM = 1; else {ieDOM = document.all; if (ieDOM) aDOM = 1;
else {
var nsDOM = ((navigator.appName.indexOf('Netscape') != -1)
&& (parseInt(navigator.appVersion) ==4)); if (nsDOM) aDOM = 1;}}
function xDOM(objectId, wS) {
if (stdDOM) return wS ? document.getElementById(objectId).style:
document.getElementById(objectId);
if (ieDOM) return wS ? document.all[objectId].style:
document.all[objectId];
if (nsDOM) return document.layers[objectId];
}

function hideColumn()
{
var objs = xDOM('menu_column',1)
var objs2 = xDOM('imgArrow',1)
if ( objs.display != 'none' )
{
objs.display = 'none';
objs2.src = 'App_Themes/Trmis/WebResource4.gif'
}
else
{
objs.display = 'block';
objs2.src = 'App_Themes/Trmis/WebResource3.gif'
}

return true;
}

</script>

Any help will be appreciated.

Thanks,

Burak

May 19 '06 #1
9 1861
what brower(s) are you testing with? Are you getting any specific
errors in the javascript console?

May 19 '06 #2

I am using IE 6.0 and I am not getting any errors in the browser.

what is the javascript console?

May 19 '06 #3
in firefox if you go to Tools | JavaScript console, you get some really
helpful javascript debugging info. much more specific than IE

May 19 '06 #4
replace all your script with this script and let me know if it works:
<script language="javascript" type="text/javascript" >

function hideColumn()
{

var objs = document.getElementById('menu_column').style;
var objs2 = document.getElementById('imgArrow');
if ( objs.display != 'none' )
{
objs.display = 'none';
objs2.src = 'App_Themes/Trmis/WebResource4.gif'
}

else
{
objs.display = 'block';
objs2.src = 'App_Themes/Trmis/WebResource3.gif'

}

return true;

}

</script>

May 19 '06 #5

Thanks Walton, it worked. :)

May 19 '06 #6
Walton said the following on 5/19/2006 10:32 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

<URL: http://www.safalra.com/special/googlegroupsreply/ >
replace all your script with this script and let me know if it works:


Looks like a lot of trouble to simply change the source of an image.

function swapPic(){
document.images['imagename'].src=document.images['imagename'].src.match('WebResource4.gif')?'WebResource3.gif': 'WebResource4.gif';

}

It could be made generic very simply though.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 19 '06 #7
Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

<URL: http://www.safalra.com/special/googlegroupsreply/ >

Thanks for the head's up. I'm new to this kind of stuff.


Looks like a lot of trouble to simply change the source of an image.

If you look at the script again, you'll notice he's not just changing
the source of an image. he also wants to hide/unhide a column, which
may or may not be referenced easily relative to the image element. So
there is a bit more to it than your solution.

function swapPic(){
document.images['imagename'].src=document.images['imagename'].src.match('We*bResource4.gif')?'WebResource3.gif' :'WebResource4.gif';
}


I never knew about the document.images properties before... is it
compatible in most browsers?

May 19 '06 #8
Walton said the following on 5/19/2006 4:20 PM:

<snip>
Looks like a lot of trouble to simply change the source of an image.

If you look at the script again, you'll notice he's not just changing
the source of an image. he also wants to hide/unhide a column, which
may or may not be referenced easily relative to the image element. So
there is a bit more to it than your solution.


Then you add a second function that I have posted twice in the last week
or so:

function changeVisibility(elem,visibilityMode){
document.getElementById(elem).style.visibility = visibilityMode;
}

And call it appropriately with the element ID and the visibility Mode
you want, whether visible or hidden.

But to be honest, I stopped reading his text (I don't want to call it
code or crap) when I read the part with navigator.appName as 99.99% of
code that is based on the navigator.appName needs to be re-written as it
is crap code.
function swapPic(){
document.images['imagename'].src=document.images['imagename'].src.match('We*bResource4.gif')?'WebResource3.gif' :'WebResource4.gif';
}


I never knew about the document.images properties before... is it
compatible in most browsers?


NN4, IE4> Any Mozilla browser and a ton more. It is a lot more
compatible than gEBI is, that is for sure.

But, document.images is a collection, not a property.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 19 '06 #9
Burak Gunay wrote:
In my asp.net 2.0 page, I have a hyperlink with an arrow image

<a id="imgtest" href="#"onclick="return hideColumn()">
<img id="imgArrow" src="App_Themes/Trmis/WebResource3.gif"
alt="Click" />
</a>
This is XHTML markup. XHTML UAs most certainly already support the
`onclick' attribute for `img' elements; you do not need a not gracefully
degrading a[href=#,onclick]:

<img src="App_Themes/Trmis/WebResource3.gif" alt="Click"
id="imgArrow" onclick="return hideColumn();" />

See also <URL:http://jibbering.com/faq/>

Note however, that IE does not support XHTML. AFAIK, it is the major
drawback of ASP.NET that it generates XHTML markup code that is served
as text/html and therefore relies on error correction to be properly
displayed. That is Microsoft's idea of proper XHTML support ...

<URL:http://hixie.ch/advocacy/xhtml>
WebResource3.gif is an arrow pointing left and when I click on it I
want to replace it with WebResource4.gif which is an arrow pointing
right, but for some reason the code below is not working.


Well, the code is simply junk.

<URL:http://pointedears.de/scripts/test/whatami>
And prophylactic: <URL:http://validator.w3.org/>
PointedEars
--
The English government is much of a German poodle as
other governments. The Germans infiltrated them all.
-- "The only real Barbara Schwarz", dsw.scientology,
<16**************************@posting.google.com >)
May 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
13045
by: Gregory | last post by:
Hello, I'm trying to do the above in order to process an image and return the result to an html image control. It fails and my key suspects are either the variable that I'm passing in -...
12
9667
by: bhennon | last post by:
Hey all, I have a small php script that calls a random image at the following page. http://www.2006ymcanationals.com/random.php IT WORKS IF I go directly to the above link. I am trying to...
8
2991
by: Eric Bragas | last post by:
What is this? <img src="{$ ImagesDir}/photo.gif"> I KNOW what an HTML image tag looks like. But what do you call that in the file source? Is it like a virtual directory in IIS? It's some type...
24
3455
by: Charles Crume | last post by:
Hello; My "index.htm" page has 3 frames (content, navigation bar, and logo). I set the "SRC" of the "logo" frame to a blank gif image and then want to change it's contents after the other two...
22
2499
by: David. E. Goble | last post by:
Hi All; I have a few of these; sigsImages="sigs/finished1.jpg" sigsImages="sigs/foghorn.jpg" lower I have;
10
31650
by: FX | last post by:
I wanna publish a script on my site which allows me to hide image source. i have rough idea abt it. i`ll point src to some php page like: <img src="image.php"> & in tht php wat exactly shud be...
5
3066
by: enaz | last post by:
Ok, so, this is my first post, if there is any info i am leaving out please tell me this is the function: function mouseOver(c) { alert(c); <!-- just for debugging--> if (c.src ==...
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.