1

I want to make texts from a database available in Javascript. I have made Texts.aspx which outputs Javascript code, and I want to include it. The includes have to be done in the right order, because Texts.aspx uses functions from other Javascript files. I tried adding a ScriptReference to this aspx file, but this breaks things. The following code gives a 404 error on ScriptResource.axd.

 <asp:ScriptManager runat="server" EnableScriptGlobalization="true">
     <CompositeScript>
         <Scripts>
             <asp:ScriptReference Path="~/Scripts/textFunctions.js" />
             <asp:ScriptReference Path="~/Scripts/Texts.aspx" />
             <asp:ScriptReference Path="~/Scripts/useTexts.js" />
        </Scripts>
    </CompositeScript>
</asp:ScriptManager>

How do include a dynamically generated Javascript file?

1
  • I solved this by adding <script> tags to the master page. I could not get the ScriptReference to an URL working. Commented Sep 28, 2011 at 8:59

2 Answers 2

1

Please use IHttpHandler instead of standard aspx page as it is what you are really look for. If you have to use aspx file be sure that you are returning only javascript content there and also that you set content type as it should be for javascripts.

For example:

public class Texts : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/javascript";
        context.Response.Write("var texts = { Some: 'some text here' }");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Than in master or aspx

<asp:ScriptManager runat="server" ID="sm">
    <Scripts>
        <asp:ScriptReference Path="/Scripts/jquery-1.4.1.js" />
        <asp:ScriptReference Path="/texts.ashx" />
    </Scripts>
</asp:ScriptManager>

and finally you can confirm it works (also you will see in firebug that you ashx file is correctly recognized as javascript)

<script type="text/javascript" language="javascript">
    alert(texts.Some);
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

No, this is not possible. A ScriptReference makes a reference to a set of script which are then read from disk, so it is not possible to make a ScriptReference to a HTTP location.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.