How to Add script codes before the </body> tag ASP.NET

Heres the problem,

In Masterpage, the google analytics code were pasted before the end of body tag.

In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript and put it before the end of body tag, because this script uses the google analytics variables.

How can i achieve this?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

An inelegant but simple approach, if you’re able to avoid using ClientScript, would be to just stick a literal in the page where you want your script to be rendered and put the script in there from your codebehind (i.e. myLiteral.Text = "client script";).

Solution 2

Take a look at RegisterStartupScript

The script block will be placed right above the </form> tag at the end of the page.

Solution 3

You could delay your script execution until after the page loads, then it won’t matter where your script resides.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply