Can I write ASP code in a .js file. Or if there is any way through which I can update database in a .js file.
问题:
回答1:
Technically, yes.
You need to add the js extension to IIS and the web.config file:
<system.web>
<httpHandlers>
<add verb="*" path="*.js" type="System.Web.UI.PageHandlerFactory"/>
</httpHandlers>
The other option (which I recommend) is request an .asp file, like this:
<script type="text/javascript" src="yourASPfile.asp"></script>
And yourASPfile.asp would send a javascript header:
<%
Response.ContentType = "application/x-javascript"
%>
Although technically, you don't need to set the header.
Now you can write asp/javascript and it gets parsed by the server THEN delivered to the browser to be used.
回答2:
If you mean that you want to use server-side scripting to generate your .js
files, Dan's answer helps you there.
If you mean you want to use Javascript as your server-side ASP language, yes, you can do that. You can set it up by configuration (changing the default langauge from VBScript to JScript), or you can do it explicitly per-file. I do the latter, just because the overwhelming assumption is that .asp
pages are written in VBScript. Here's an example:
<%@Language=JScript%>
<!DOCTYPE HTML>
<html>
<head>
<title>Example Javascript ASP Page</title>
</head>
<body>
<%
var hello;
hello = "Hello from Javascript!";
Response.Write("<p>" + hello + "</p>");
%>
</body>
</html>
I do this in any Classic ASP app I write, because that way I'm using the same language on the client and the server. I frequently code up a piece of abstract functionality that ends up being in both places (via includes). Very efficient. :-)