2011-06-29 35 views
1

データベースのテキストをJavascriptで利用可能にしたい。私はJavascriptコードを出力するTexts.aspxを作ったので、それをインクルードしたい。 Texts.aspxは他のJavascriptファイルの関数を使用するため、インクルードは正しい順序で実行する必要があります。私はこのaspxファイルにScriptReferenceを追加しようとしましたが、これは事態を打破します。次のコードは、ScriptResource.axdの404エラーを示しています。ScriptReferenceを動的スクリプトに追加する

<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> 

どのように動的に生成されたJavascriptファイルを含めるには?

+0

マスターページに「

1

あなたが本当に探しているように、標準のaspxページの代わりにIHttpHandlerを使用してください。 aspxファイルを使用する必要がある場合は、javascriptコンテンツのみを返し、javascriptのコンテンツタイプを設定するようにしてください。例えば

マスターまたは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> 

よりも

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; 
     } 
    } 
} 

、最終的にあなたはそれが動作を確認することができます(また、あなたはファイルが正しくジャバスクリプトとして認識されASHX放火犯に表示されます)

<script type="text/javascript" language="javascript"> 
    alert(texts.Some); 
</script> 
関連する問題