Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to add EL tags in a javascript function?

 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
function ()
{displayMessage( ${sessionScope.Object.val} + someval.getrowcount(); }
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you trying to achieve?
 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koen Aerts wrote:What exactly are you trying to achieve?

I have a javascript function on a page. I would like to access some session variable within that function
 
J. Kevin Robbins
Bartender
Posts: 1725
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh Khan wrote:function ()
{displayMessage( ${sessionScope.Object.val} + someval.getrowcount(); }


No. Javascript runs on the client. EL is evaluated by the server and converted to html before it's sent to the client. By the time the client gets the page, EL doesn't exist anymore. Look at the page source to understand what EL does.

You didn't give any explanation of what you are trying to accomplish, but I can take a guess. Consider whether you can populate a hidden field on your page using EL. Then the Javascript will be able to access the value of a hidden field. If you need more advice you'll have to give a more detailed explanation of what you are trying to do.

 
J. Kevin Robbins
Bartender
Posts: 1725
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh Khan wrote:
Koen Aerts wrote:What exactly are you trying to achieve?

I have a javascript function on a page. I would like to access some session variable within that function


We cross-posted. It sounds like the hidden field trick will do what you need.

The important thing to remember is that Java/JSP/JSTL/EL all happens on the server. Nothing ends up at the client except html, and you can view the source of that with your browser. That html is what Javascript has to work with, and that's ALL it has to work with.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the EL can be used to help create the JavaScript markup.

For example: var x = '${someValue}';
is perfectly valid. So if someValue has the toString() value of whatever, the statement that will be sent to the browser is:
var x = 'whatever';
 
J. Kevin Robbins
Bartender
Posts: 1725
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:But the EL can be used to help create the JavaScript markup.

For example: var x = '${someValue}';
is perfectly valid. So if someValue has the toString() value of whatever, the statement that will be sent to the browser is:
var x = 'whatever';


Ah, yes, good point. I was thinking strictly in terms of external JavaScript files. I rarely use embedded JavaScript code. Lack of imagination on my part. I stand corrected.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it is a good point that this is only possible in JSP files and not any other resource type.
 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats what i was thinking.. since the server evaluates everything on a page translating the EL along the way. I could use EL inside a javascript function to do something like this



This would be possible right?

 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do that, yes.

I wouldn't describe it as "using EL inside a Javascript function" because to me the Javascript function doesn't exist, or at least is of no consequence, until it's in the browser. That phrase also implies to me that the EL would be "used" when the function is executed, which is not the case. I would describe it as "using EL while generating a Javascript function".
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you need to make sure that the generated text -- because that's all it is on the server -- results in valid JavaScript syntax.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic