473,799 Members | 3,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9993
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.

HtmlGenericCont rol jsInclude = new HtmlGenericCont rol("script");
jsInclude.Attri butes.Add("type ", "text/javascript");
jsInclude.Attri bues.Add("src", "myJavascriptFi le.js");
this.Page.Heade r.Controls.Add( jsIncude);

HtmlGenericCont rol masterPageBodyT ag =
(HtmlGenericCon trol)Page.Maste r.FindControl(" MainBody");
masterPageBodyT ag.attributes.A dd("onLoad","my javascriptfunct ion();");
------------------

On other thing, if you want something different for a PostBack you
will need to add an else clause and put in
masterPageBodyT ag.Attributes.R emove("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
1307
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
1479
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 I am a javascript newbie I find the site a bit confusing and difficult to navigate around. I have also tried other sites, and find them equally confusing - I'm sure they are good for people who understand what they are searching for though.
1
1754
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 console. What i found out is that FireFox doesnt recognize Javascript functions from a xsl page. Its ok from a html page. I have given a code of a xsl page. Can someone point what possibly could be the problem. <?xml version="1.0"...
3
3942
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 class library that contains a function I use all the time (to create a link around some text). That function in turn calls a javascript function. Usually I would have put the javascript on the
1
1464
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, the <head> tag is entirely contained in the master. This means that I could put the JS functions in the master, but then they would appear on every page, which I don't want.
15
1596
by: Water Cooler v2 | last post by:
Can javascript functions have variable number of arguments - a concept known as paramarrays?
2
1163
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 control does not exist if I try to execute the JavaScript function document.getElementById... Any idea???
3
9589
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 an instance of Internet Explorer and control the DOM, but I don't know how to call Javascript functions. Here is the code I am using so far: public class WebBrowser
6
3537
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 properly, the user is supposed to be directed back to the home page, but instead the page simply seems to reload. I would assume that the form is trying to post back, however, I would like to avoid this if possible. In the past for other asp.net...
6
1339
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 this usings commands such as: <SCRIPT src="folder/functions.js" type=text/javascript></SCRIPT> However, when I try this it does not work. Can anyone explain what I need to do to make this work OR point me in the direction of a suitable tutorial...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.