Can I write ASP code in a .js file?

2019-08-02 05:51发布

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.

2条回答
你好瞎i
2楼-- · 2019-08-02 06:20

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

查看更多
干净又极端
3楼-- · 2019-08-02 06:25

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.

查看更多
登录 后发表回答