1

I saw Difference between RegisterStartupScript and RegisterClientScriptBlock from here.
There is describing the injection javascript code from Sever side using both of them.
Now I am also injecting client side script from an ASP.NET Server Control but my client script is just pointing to an external JavaScript file.

string jsString="<script src="myscripts.js"></script>"
ClientScript.RegisterClientScriptBlock(this.GetType(), "JSScriptBlock",jsString);

I am using RegisterClientScriptBlock,but I want to know should I use RegisterStartupScript to be faster. Which is faster RegisterStartupScript or RegisterClientScriptBlock in my case?
Thanks.

Community
  • 1
  • 1
nnnn
  • 1,041
  • 3
  • 18
  • 35
  • 1
    Do you experience speed issues with any of them? If not, why would you worry about speed? – sisve Oct 12 '12 at 09:42

1 Answers1

5

The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object's element. The code cannot access any of the form's elements because, at that time, the elements haven't been instantiated yet.

The RegisterStartupScript method inserts the specified client-side script just before the closing tag of the Page object's element. The code can access any of the form's elements because, at that time, the elements have been instantiated.

The choice of which method to use really depends on the "order" in which you want your script to be run by the browser when rendering the page.

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51