Skip to content Skip to sidebar Skip to footer

Treat External Javascript File As Part Of Aspx Page

I know you can't use asp net server tags in an external javascript file. This is a bit of pain, because it forces you to declare your variables that need ClientID in the aspx page

Solution 1:

I know you can't use asp net server tags in an external javascript file

You can create an ASPX page to generate dynamic javascript

<%@ Page Language="C#" AutoEventWireup="false"
CodeFile="script.aspx.cs" Inherits="scripts_script"
EnableViewState="false" StyleSheetTheme="" %>
function test() {
    testinfo.innerHTML = "<%= MyVariable %>";
}

Make sure to set StyleSheetTheme="" otherwise the runtime will insert a <head> which you don't want

And in the code behind set the ContentType to application/x-javascript

using System;

publicpartialclassscripts_script
{
    protectedoverridevoidOnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.Response.ContentType = "application/x-javascript";
    }
}

Now you can use this ASPX page as if it were a .js file.

Post a Comment for "Treat External Javascript File As Part Of Aspx Page"