• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting scriptlet in java script

 
Yans Tan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

I am facing some problem with inserting the following line in the
javascript.

out.println("document.getElementById(\"img1\").src = \"data:image/jpeg;base64,"+(String)v3.get(1) +"\";");


What I did was to out.print the whole javascript in the jsp file.
Not sure anyone know is this the right way?

Because I tried inserting scriplet in javascripts and its giving me some problem.

Thanks in advance for helping all!
Yans
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its giving me some problem.

What does this mean? What is the HTML/JavaScript that gets generated?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JSP forum.
 
Stefan Evans
Bartender
Pie
Posts: 1670
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This approach is fine, as long as you understand the separation of responsibility. Your JSP will produce javascript code onto the page.
Javascript code will not be accessing java variables.

The easiest way to debug this is to generate the page, and then view source to see what the javascript generated was.
Sometimes I even save that as a static page, edit the javascript until it works, and then go back and change the JSP so it produces the correct javascript.
 
Yans Tan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thanks for all your help!

I found out that the problem actually lies with the base64.

But I have a problem inserting javascript variable in the scriplet

For example:

out.println("document.getElementById(\"img1\").src = \"data:image/jpeg;base64,"+(String)vec3.get(1)+"\";");

(String)vec3.get(1)


How do I change the 1 to a javascript variable?

Thanks in advance for all the help!

Regards
Yans
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this?

 
Yans Tan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

The code that i quoted has no error. But i want to change the number in the vector.get() to a javascript variable.

How do I insert a javascript variable in my vector.get(). It will be recongised as a string if I am not wrong. Anyway to solve this anyone?

Thanks for all the help!

Regards
Yans
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't change it to a Javascript variable because Javascript will be interpreted in the client (browser) AFTER all of the JSP code has finished and the output is sent to the client.


To better understand this, run your code and then use your browsers 'view source' option to see what was returned from the server.
THAT is the code that the Javascript interpreter will see.
[ August 03, 2006: Message edited by: Ben Souther ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This article might be helpful in understanding the relationship (or lack thereof) between Javascript and server-side Java.
 
Yans Tan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

Thanks for the article.
I think this approach is wrong. Anyone can suggest how to counter this?
I shall have a more detailed description of what I am trying to do:

1) Use javascript to bind fuctions to the up key on the keyboard
(case 38 i think)

2) So what is suppose to happen is that the image is suppose to change when user presses up key.
Code behind:
case 38:
image++
document.getElementById('img1').src = data:image/jpeg;base64,vector(image);

So I was thinking of another way that might work, which is to have a javascript array, then hard code the size and initilising of values.
But that would not be practical.

Anyone have any better ways to do it? Forgive any syntax errror i made. I was just citing an example.

Thanks for helping!

Regards
Yans
[ August 03, 2006: Message edited by: Yans Tan ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
document.getElementById('img1').src = data:image/jpeg;base64,vector(image);


I do not understand what this line is supposed to do. The src attribute of the the <img> tag must be a URL to the image file or a servlet that will serve the image data.

You're not trying to in-line the image data in the JSP are you? That's not going to work.
 
Yans Tan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

Sorry I not sure what is in-line.

Anyway that line is supposed to display an image throught the vector.get(variable) which is from another bean. Its working fine with me. Sorry for any confusion from my syntax again.

I think I managed to solve it through looping:

for(int z=0;z<=2; z++)
{

out.println("sa["+z+"]="+ array[z] +";");
}

This was what I did. Thanks for all your advice and help!
Thanks alot!

Regards
Yans
 
Yans Tan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway i would like to make some comments.

I really find this place very useful and the people here are really friendly and gives good advices.
It really saves me much trouble looking blindly on the net.
Especially Bear, you really help me alot Thanks!

Take care all! Thanks again

Regards
Yans
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic