Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Point to an image in a javascript

 
Talgat Karylgashov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everybody!
I need help.
My task is to point to a .png file in a javascript. If I do this in the .html using relative path to the .png which is located in the /images folder everything is OK, but in the .jsp the javascript function can't find the image. I tried to use relative path, then tried to use
but javascript can't find image at all.

Thanks in advance.
 
Jorge Pinho
Greenhorn
Posts: 25
MySQL Database PHP Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Your are mixing things up!

Javascript is NOT Java

JSP still is Java

What exactly is the HTML output of that code ?

 
Talgat Karylgashov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to do a dynamic counter on the JSP. "Dynamic" means by javascript. I use some script which manipulates PNG images to show the counter. Pathes to this PNG images must be defined explicitly in the javascript code. So, in the simple HTML (if I place script into <head/> tags, then define relative pathes to images) it works. But if I move it to my JSP, images a not shown.
Maybe I should use servlet which will transfer an array of bytes of PNGs or something like that?..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64985
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's still not clear what you are trying to do, or if you even understand the difference between Java and JavaScript. The code that you have shown in your first post is Java, not JavaScript.

Please tell us, clearly, what you are actually trying to accomplish.
 
Talgat Karylgashov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. This is my javascript function which works in the .html.

I need to make it work in the .jsp (javascript can be placed into .jsp). If I place 'numeric_up_blackbg5.png' and 'marker.png' images in the same folder where the .jsp is located it doesn't work (I do not see the counter).
.jsp is the main page of my web project.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64985
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the JspFaq. Resource URLs (suc as images) should be server-relative beginning with the context path.
 
Talgat Karylgashov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, thanks for your answer, but the JspFaq doesn't help me.
If you look at my first post you will see that I tried to use context path and the javascript code in my .jsp is below.

But anyway I can't see this two .png images when I start my web project and open the .jsp.
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this works:

Talgat Karylgashov wrote:



and this doesn't

Talgat Karylgashov wrote:


Is that the issue?
What is the generated source for the JSP?
And what is your directory structure in the server?
Can you access directly in the browser adress http://yourserver:port/contextPath/images/marker.png?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64985
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's far past the time to use a client-side debugger and examine the DOM elements (not the source) representing the images. Inspect the URLs to the resource to see what the issue is.
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I strongly agree with you but I must say that I have faced situations (this week by the way) where a colleague asked for my help because something "was not working" and when I tried it I saw a big red error in Firebug and asked my colleague "do you use Firebug?", "yes", "how could you miss the error console's error while you were working with javascript?", "..."

So life has tought me not to assume that everybody uses Firebug, chrome developer tools or others.

Nice story to tell my grandchildren in the future though :P
 
Talgat Karylgashov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, guys!
I've solved the problem.
It's in the javascript's behavior. If I put the <div id="counter_0_1"></div> used to show the counter befor the javascript's code it works. And doesn't work if I put the <div> after the javascript. Weird...
Sorry if I wasted your time. It has taught me to compare working and non-working code carefully before posting to a forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64985
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Talgat Karylgashov wrote:And doesn't work if I put the <div> after the javascript. Weird...

Not weird at all -- you can't manipulate elements that haven't been created yet.

You might want to familiarize yourself with the concept of load and ready handlers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic