tags:

views:

101

answers:

2

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.

+3  A: 

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.

Dan Heberden
You could also just create a regular asp script that outputs JS (including the `content-type` header) and then route a fake JS file to it. For example, you could set up a routing rule such that `http://foo/js/dynamic/$bar.js` is routed to `http://foo/generate_js.asp?param=$bar`.
Lèse majesté
+1  A: 

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. :-)

T.J. Crowder
Very interesting...
Lèse majesté