User Name Password Register
DaniWeb IT Discussion Community
All
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 402,788 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,793 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: 202 | Replies: 3
Reply
Join Date: Jun 2005
Posts: 2
Reputation: vikaspa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vikaspa vikaspa is offline Offline
Newbie Poster

java script help want to use it differently

  #1  
Jul 29th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Location: New Jersey
Posts: 46
Reputation: chaosprime is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
chaosprime's Avatar
chaosprime chaosprime is offline Offline
Light Poster

Re: java script help want to use it differently

  #2  
Jul 29th, 2008
Try this: 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.
Chaos
Lost Souls: text based RPG
MUDseek: MUD gaming search
Reply With Quote  
Join Date: Jun 2005
Posts: 2
Reputation: vikaspa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vikaspa vikaspa is offline Offline
Newbie Poster

Re: java script help want to use it differently

  #3  
Jul 29th, 2008
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
Reply With Quote  
Join Date: Jul 2008
Location: New Jersey
Posts: 46
Reputation: chaosprime is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
chaosprime's Avatar
chaosprime chaosprime is offline Offline
Light Poster

Re: java script help want to use it differently

  #4  
Jul 29th, 2008
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:
    document.someName.src = mobilebigimage.jpg
This should be what you're seeing in your alerts. This will not work. You need to be doing this:
    document.someName.src = 'mobilebigimage.jpg'
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:
  1. <javascript>
  2. <script language="javascript">
  3. function ShowPic(imgname, sImage) {
  4. document[imgname].src = sImage;
  5. }
  6. </script>

If I'm not horribly mistaken, that will work far better and with much less fuss.
Chaos
Lost Souls: text based RPG
MUDseek: MUD gaming search
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 10:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC