Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diff RegisterClientScriptBlock & RegisterStartupScript & responce.write("script")

i need individual explanation and individual advantages regarding RegisterClientScriptBlock & RegisterStartupScript & responce.write("script");

i got some information like

RegisterClientScriptBlock() methods will inject the script after the form open tag but before page controls

RegisterStartupScript() methods will inject the scripts after page controls but before form close tag.

we can also simply wrie responce.write("script") it aslo include the script.

but i need individual explanation

if any link is available regarding this topic also pls reply me.

like image 323
vineth Avatar asked Feb 17 '26 05:02

vineth


1 Answers

If you use the RegisterClientScriptBlock() and you need to reference other javascript methods or html objects that may get injected, you run the risk of them not being created when the script is run. Using the RegisterStartupScript(), as you said, puts the script at the bottom of the page, garanteeing that all objects will have been rendered and created before your script is run.

If you are using either to add a script that is a function like:

<script type="text/javascript>
    function myFunc(){
    ...
}
</script>

Then is doesn't matter, because that function needs to be called explicitly, by something else.

But if you are trying to have a script run after the document loads like:

<script type="text/javascript>
    doStuff();
</script>

Then you should use RegisterStartupScript() so you are garanteed that any objects called by you or the functions you call exists.

like image 70
alanquillin Avatar answered Feb 18 '26 18:02

alanquillin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!