0

ページにajax JavaScript が既にある場合に、JavaScript ファイルを埋め込みリソースとしてページに追加するにはどうすればよいですか? NBは、コードをユーザーコントロールにラップするために、この部分を動的に実行したいと考えています。

aspx ページ: scriptmanager

コード: アセンブリ: WebResource("Functions.js", "text/javascript")

コード: onPreRender:

ScriptManager.RegisterClientScriptResource(Me.Page, Me.GetType().BaseType, "Functions.js")

このコードは私の JavaScript コードをページに正常に追加しますが、AJAX JavaScript の後ではなく、すべての機能が正しく機能するとは限りません。

ありがとう、デイブ

4

1 に答える 1

1

You could use GetWebResourceUrl() to get the path string to your embeded script as it should be in the page (probably something like websresource.axd?XXXX). Then manually insert a script tag pointing to the path using RegisterStartupScript to force it to be added at the end of the page. Something like below...

path = ScriptManager.GetWebResourceUrl(this.GetType(), "Fucntion.js");
ScriptManger.RegisterStartupScript(this.GetType(), "MyScript", "<script type=\"text/javascript\" src=\"" + path + "\"/>");

From the server you could also try adding your script later in the load process for example in the prerender event. Or from the client side can wrap your script into a load so that it doesn't run the code until the last possible moment as below...

function RunOnLoad()
{

     button.onclick = function()
     {
       alert("Clicked!");
     }

}
window.onload = RunOnLoad;
于 2010-07-12T10:33:33.260 に答える