| Author |
Is it possible to add EL tags in a javascript function?
|
Rajesh Khan
Ranch Hand
Joined: Oct 16, 2011
Posts: 224
|
|
function ()
{displayMessage( ${sessionScope.Object.val} + someval.getrowcount(); }
|
 |
Koen Aerts
Ranch Hand
Joined: Feb 07, 2012
Posts: 339
|
|
|
What exactly are you trying to achieve?
|
 |
Rajesh Khan
Ranch Hand
Joined: Oct 16, 2011
Posts: 224
|
|
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
|
 |
Jk Robbins
Ranch Hand
Joined: Dec 16, 2010
Posts: 159
|
|
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.
|
 |
Jk Robbins
Ranch Hand
Joined: Dec 16, 2010
Posts: 159
|
|
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 opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50693
|
|
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';
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Jk Robbins
Ranch Hand
Joined: Dec 16, 2010
Posts: 159
|
|
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 opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50693
|
|
|
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: 224
|
|
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: 13842
|
|
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 opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50693
|
|
|
Also, you need to make sure that the generated text -- because that's all it is on the server -- results in valid JavaScript syntax.
|
 |
 |
|
|
subject: Is it possible to add EL tags in a javascript function?
|
|
|