wood burning stoves 2.0*
The moose likes JSP and the fly likes Point to an image in a javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Point to an image in a javascript" Watch "Point to an image in a javascript" New topic
Author

Point to an image in a javascript

Talgat Karylgashov
Greenhorn

Joined: Oct 12, 2011
Posts: 7
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

Joined: May 16, 2003
Posts: 25

Hi,

Your are mixing things up!

Javascript is NOT Java

JSP still is Java

What exactly is the HTML output of that code ?


Jorge Pinho / SCJP 5
Live Help via Skype - lets.talk.about.help
Dont wait for forum answers
Talgat Karylgashov
Greenhorn

Joined: Oct 12, 2011
Posts: 7
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

Joined: Jan 10, 2002
Posts: 61095
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Talgat Karylgashov
Greenhorn

Joined: Oct 12, 2011
Posts: 7
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

Joined: Jan 10, 2002
Posts: 61095
    
  66

See the JspFaq. Resource URLs (suc as images) should be server-relative beginning with the context path.
Talgat Karylgashov
Greenhorn

Joined: Oct 12, 2011
Posts: 7
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

Joined: Apr 08, 2009
Posts: 298
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

Joined: Jan 10, 2002
Posts: 61095
    
  66

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

Joined: Apr 08, 2009
Posts: 298
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

Joined: Oct 12, 2011
Posts: 7
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

Joined: Jan 10, 2002
Posts: 61095
    
  66

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Point to an image in a javascript