473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Add JavaScript functions to body tag

In an ASP.NET 2.0 application using masterpages, how can I add a 'load' and
'unload' JavaScript function to the body tag for some aspx page that uses
the masterpage. I don't want to add it to the masterpage because it's just
for one page.

Have a nice day
Kees

May 21 '07 #1
2 9973
On May 21, 11:51 am, "Kees de Winter" <nos...@nospam.orgwrote:
In an ASP.NET 2.0 application using masterpages, how can I add a 'load' and
'unload' JavaScript function to the body tag for some aspx page that uses
the masterpage. I don't want to add it to the masterpage because it's just
for one page.
I think something like this

1. Turn <bodyinto a server side control

<body runat="server" id="body">

2. Add a public method

public void SetBodyOnLoad()
{
body.Attributes("onload") = "javascript:....";
}

3. Call that method from the content page

May 21 '07 #2
Welcome to the horror that is asp.net and working with an outside of
microsoft standard such as javaScript.

The easist way is to put your javascript into a standard *.js file and
then load that, then call the function.

Add the following to your Page_Load, on my pages ID, in the master
page, of the body tag is "MainBody". The adds the javascript file so
it is load by the client browser and then add a function to the body
onLoad.

HtmlGenericControl jsInclude = new HtmlGenericControl("script");
jsInclude.Attributes.Add("type", "text/javascript");
jsInclude.Attribues.Add("src","myJavascriptFile.js ");
this.Page.Header.Controls.Add(jsIncude);

HtmlGenericControl masterPageBodyTag =
(HtmlGenericControl)Page.Master.FindControl("MainB ody");
masterPageBodyTag.attributes.Add("onLoad","myjavas criptfunction();");
------------------

On other thing, if you want something different for a PostBack you
will need to add an else clause and put in
masterPageBodyTag.Attributes.Remove("onLoad").
For some reason asp.net likes to cache the onLoad so if you have the
include so that on a PostBack it is not loaded you will still have the
onLoad call sent to the client.

May 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Priya | last post by:
Hi, I'm developing a windows application in VC#.It has a web browser control in it.How can I call the javascript functions present in the web page? Thanks in advance Priya
2
by: Dot net work | last post by:
Is toString() compatible with all browsers? I don't know where to look on the internet to find out what javascript functions are ok to use with what browsers. I tried www.quirksmode.org but as...
1
by: Rose Girl | last post by:
I have a web application that uses xsl and javascript which runs smoothly on IE. Recently i decided to try it on FireFox 1.0 . But the same application gives javascript errors as displayed in the...
3
by: Nikki | last post by:
Hi, I would like to create a central storage place for javascript functions that I can then access from any of my asp.net projects. An example of what I need it for is as follows: I have a...
1
by: Alan Silver | last post by:
Hello, I am just converting some pages to use master pages, and I have come across a bit of a problem. One of the pages uses some custom Javascript functions for client-side validation. Now,...
15
by: Water Cooler v2 | last post by:
Can javascript functions have variable number of arguments - a concept known as paramarrays?
2
by: Bruce One | last post by:
When I put an UpdatePanel (Atlas control) araound my ServerControls they all seem to vanish from the page (I cant find them in my source page...). So the browser fires an exception telling the...
3
by: Matthew Lock | last post by:
Hello, I am automating Internet Explorer in order to do some simple automated testing of a web application. How do I invoke Javascript functions in the web page I load? I can successfully start...
6
by: mcfly1204 | last post by:
I have a page that contain input form, a contact us page. I also have a few javascript functions, one in particular sends the visitor back to the home page. My javascript is not functioning...
6
by: Blackmore | last post by:
I'd like to screate a link to my javascript functions, such that they do not appear in the page source code and users cannot tamper with them. I've noticed that other websites appear to be able to...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.