Rod
Rod

Reputation: 15457

Why does this code use an .aspx file for JavaScript?

I found some old code which I'm not sure I understand completely. The folowing is an .aspx page containing only JavaScript:

<%@ Page Language="C#" EnableSessionState="True" CodePage="65001" uiculture="auto" %>

<%
Response.ContentType = "text/plain";
%>


var csBackgroundColor;

function testfx() {
    csBackgroundColor.setAttribute('disabled', 'disabled');
}

and it was referenced like this:

<script type="text/javascript" src="filename.js.aspx"></script>

I'm wondering why it wasn't just marked as completely a JavaScript file? Was it done this way so you could include code blocks? With the file this way, I don't even get IntelliSense.

Upvotes: 3

Views: 135

Answers (2)

Claudio Redi
Claudio Redi

Reputation: 68400

First time I see something like that but you're right, the reason behind this is to make the file to be processed by the asp.net engine and run it as any other aspx page, giving him the chance to use server side code to build the js file.

If that's all the code in the file, it seems the only objetive was to set the character encoding (CodePage="65001" and the content type (Response.ContentType="text/plain"). It wouldn't make a lot of sense as you can imagine.

Upvotes: 5

Alex
Alex

Reputation: 35409

<hack>To get intellisense you could wrap the code in <script> ... </script> blocks and remove them before saving.</hack>

This was probably created by a developer that didn't want to, couldn't, or didn't have the time to write object oriented JavaScript. Encapsulating the logic and configuring through a configuration object would allow you to de-couple the code from external variable dependencies, (e.g. var foo = <%= SomeServerSideVariable %>).

Upvotes: 3

Related Questions