Tim,

"Does .js files not execute jsp code?"

No, .js does not excute jsp code. Only .jsp executes the JSP code.

There are two types of includes in JSP.
1. file include:- the file is included before the JSP is compiled into servlet.
2. page include:- this is a dynamic runtime include, the page (.js file) is included 
when the request is processed.

Here are the examples I created:

*************************************************
Example 1: Using file include, the code works for string literals and not with string 
variables.
*************************************************

**************** testjs.jsp *********************
<%@ include file="hello.js" %>

<% String ab = request.getParameter("a");%>
<html>
<head>
</head>
<body>
   TestJS
<br>
<br>
<a href='javascript:sayHi()'>Say Hi</a>
</body>
</html>

***************  hello.js ***********************
//This works because "a" is String literal
// Result in alert is a
<SCRIPT LANGUAGE="JavaScript">
function sayHi()
{
 alert( '<%= "a" %>');
}
</script>


***************  hello.js ***********************
//This does not work because it gives compile error as variable a not found.
<SCRIPT LANGUAGE="JavaScript">
function sayHi()
{
 alert( '<%= a %>');
}
</script>

*************************************************
Example 2: Using jsp page include, the JSP code is in hello.js is not compile. and the 
result is <%= "a" %> and not a.
*************************************************

**************** testjs.jsp *********************
<%@ include file="hello.js" %>

<% String ab = request.getParameter("a");%>
<html>
<head>
</head>
<body>
   TestJS
<br>
<br>
<a href='javascript:sayHi()'>Say Hi</a>
</body>
</html>

***************  hello.js ***********************
//This works because "a" is String literal
// Result in alert is a
<SCRIPT LANGUAGE="JavaScript">
function sayHi()
{
 alert( '<%= "a" %>');
}
</script>


The only way to achive this is to assign the value to a hidden value and then pass the 
value to the javascript.

Hope this explains what you needed to know.

Peace,
Jay

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to