wood burning stoves 2.0*
The moose likes JSP and the fly likes Is it possible to add EL tags in a javascript function? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Is it possible to add EL tags in a javascript function?" Watch "Is it possible to add EL tags in a javascript function?" New topic
Author

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

Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
function ()
{displayMessage( ${sessionScope.Object.val} + someval.getrowcount(); }
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

What exactly are you trying to achieve?
Rajesh Khan
Ranch Hand

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

Joined: Dec 16, 2010
Posts: 919
    
  13

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.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 919
    
  13

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

Joined: Jan 10, 2002
Posts: 61092
    
  66

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';


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 919
    
  13

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

Joined: Jan 10, 2002
Posts: 61092
    
  66

Actually it is a good point that this is only possible in JSP files and not any other resource type.
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 10, 2002
Posts: 61092
    
  66

Also, you need to make sure that the generated text -- because that's all it is on the server -- results in valid JavaScript syntax.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to add EL tags in a javascript function?