Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass JSP value to external Javascript file

Tags:

javascript

jsp

Is there a way that I can pass a JSP variable value to an external Javascript file by using only Javascript and JSP. Something like:

JSP

String str = "Hello";

external.js

//not working
var str = "<%=str%>";

Thanks.

like image 632
user200340 Avatar asked Jul 07 '11 09:07

user200340


2 Answers

If you think that the java script is written inside the JSP then its possible.

But you are providing the JS file separately then you can make a global variable and that variable will be available everywhere.

like image 160
Talha Ahmed Khan Avatar answered Sep 18 '22 11:09

Talha Ahmed Khan


Your not going to be able to write to the external js file. However if you put var s = "<%=str%>";in your jsp s will become a global variable which can be used within your external js file because s will have a global scope. So in a nutshell, you can't modify the actual js file, but you can add a variable with global scope and reference that variable within your external js script.

like image 34
Kevin Bowersox Avatar answered Sep 17 '22 11:09

Kevin Bowersox