•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 397,720 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,562 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 193 | Replies: 3
![]() |
•
•
Join Date: Jun 2005
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Need your help in javascript
I am using following script for image over (to display another image)
<html>
<img src="mobilesmallimage.jpg" name="ShowRoom" width="129" height="96" onMouseOver=" ShowPic(name,'mobilebigimage.jpg') " onmouseout="ShowPic(name,'mobilesmallimage.jpg')" border="0"> </a>
I am sending 2 parameters
1. Name of image - variable name
2. Name of image to be displayed
Using following javascript
<javascript>
<script language="javascript"> function ShowPic(imgname, sImage) {
var Imgname=imgname;
var sImage = sImage ; j='document.' +Imgname+'.src = '+sImage; alert(j); eval('document.' +Imgname+'.src = '+sImage);
; } </script>
alert shows me correct value i.e. the image to be dispayed however the image is not displayed here
However following set of code works fine
<img src="mobilesmallimage.jpg" name="ShowRoom" width="129" height="96" onMouseOver="ShowPic('mobilebigimage.jpg')" onmouseout="ShowPic('mobilesmallimage.jpg')" border="0"> </a>
and javascript
<script language="javascript"> function ShowPic(sImage) {
sImage = sImage ; document.ShowRoom.src = sImage; } </script>
Please help
Thanks in advance
I am using following script for image over (to display another image)
<html>
<img src="mobilesmallimage.jpg" name="ShowRoom" width="129" height="96" onMouseOver=" ShowPic(name,'mobilebigimage.jpg') " onmouseout="ShowPic(name,'mobilesmallimage.jpg')" border="0"> </a>
I am sending 2 parameters
1. Name of image - variable name
2. Name of image to be displayed
Using following javascript
<javascript>
<script language="javascript"> function ShowPic(imgname, sImage) {
var Imgname=imgname;
var sImage = sImage ; j='document.' +Imgname+'.src = '+sImage; alert(j); eval('document.' +Imgname+'.src = '+sImage);
; } </script>
alert shows me correct value i.e. the image to be dispayed however the image is not displayed here
However following set of code works fine
<img src="mobilesmallimage.jpg" name="ShowRoom" width="129" height="96" onMouseOver="ShowPic('mobilebigimage.jpg')" onmouseout="ShowPic('mobilesmallimage.jpg')" border="0"> </a>
and javascript
<script language="javascript"> function ShowPic(sImage) {
sImage = sImage ; document.ShowRoom.src = sImage; } </script>
Please help
Thanks in advance
Try this:
You are probably in dire need of debugging feedback; lots of people seem to think they have to write JavaScript using solely their imagination. I recommend loading up your page in Firefox and looking at Tools > Error Console.
eval('document.' +Imgname+'.src = \''+sImage+'\'');
You are probably in dire need of debugging feedback; lots of people seem to think they have to write JavaScript using solely their imagination. I recommend loading up your page in Firefox and looking at Tools > Error Console.
•
•
Join Date: Jun 2005
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Dear Sir
I appreciate your valued feed back
I traied eval (in javascript function)
May try in html part ?
Let me elaborate you why am asking this :
I am writing code for cms (Content Management software)
In this we could different image on the mouse over
However if data is read from database then I need to dynamically give values for name and images to be displayed on mouse over and on mouse out
to simulate this I had changed the javascript given to you
Hope this clarifies
I appreciate your valued feed back
I traied eval (in javascript function)
May try in html part ?
Let me elaborate you why am asking this :
I am writing code for cms (Content Management software)
In this we could different image on the mouse over
However if data is read from database then I need to dynamically give values for name and images to be displayed on mouse over and on mouse out
to simulate this I had changed the javascript given to you
Hope this clarifies
No, that doesn't really tell me anything useful.
What I'm saying in my post is that you need to wrap the target URL you're changing the image's source to in single quotes. You're constructing an eval that looks like this:
This should be what you're seeing in your alerts. This will not work. You need to be doing this:
The replacement eval-construction code I posted before is a way to do this.
The reason that nothing like this had to be done before, but has to be done now, is that you're changing methodologies to using an eval, which means that you have to make a string that interprets as valid JavaScript.
But then, the whole methodology you're using isn't really such a good idea. So here's another suggestion for what to do instead of any of this:
If I'm not horribly mistaken, that will work far better and with much less fuss.
What I'm saying in my post is that you need to wrap the target URL you're changing the image's source to in single quotes. You're constructing an eval that looks like this:
document.someName.src = mobilebigimage.jpg
document.someName.src = 'mobilebigimage.jpg'
The reason that nothing like this had to be done before, but has to be done now, is that you're changing methodologies to using an eval, which means that you have to make a string that interprets as valid JavaScript.
But then, the whole methodology you're using isn't really such a good idea. So here's another suggestion for what to do instead of any of this:
javascript Syntax (Toggle Plain Text)
<javascript> <script language="javascript"> function ShowPic(imgname, sImage) { document[imgname].src = sImage; } </script>
If I'm not horribly mistaken, that will work far better and with much less fuss.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
- designing an operating system (Computer Science and Software Design)
- jsp login code (JSP)
- help my computer is being hacked ! (Viruses, Spyware and other Nasties)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: How to Create Web Spider / Crawler?
- Next Thread: Ajax help => Scriptculous


Linear Mode