473,396 Members | 1,886 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,396 software developers and data experts.

JavaScript From PageLoad()?

Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css" rel="stylesheet" type="text/css"
/>

<script language="JavaScript" src="common/js/global.js"
type="text/JavaScript"></script>

</head>

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

Dec 6 '06 #1
4 15921
Well, if you've copied this exactly from your file:
function TestJS()

{

alert('Hello World!'};

}

then you have a right bracket where you should have a right paren.
Frank wrote:
Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css" rel="stylesheet" type="text/css"
/>

<script language="JavaScript" src="common/js/global.js"
type="text/JavaScript"></script>

</head>

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

.

.

.

</body>

</html>

In the individual page code behind where I wish to call the js function, it
works if I use:

protected void Page_Load(object sender, EventArgs e)

{

try

{

string temp = "alert('Hello World!');";

HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");

body.Attributes.Add("onload", temp);

}

catch (NullReferenceException x)

{

Response.Write(x);

}

}

But if I try to call a function from the js file like so:

protected void Page_Load(object sender, EventArgs e)

{

try

{

string temp = "TestJS();";

HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");

body.Attributes.Add("onload", temp);

}

catch (NullReferenceException x)

{

Response.Write(x);

}

}

It fails. I don't get an ASP.NET exception, but rather a JavaScript error
which states it is expecting an object, but I can't figure out what object
it means. My js file looks like this:

// JScript File

function TestJS()

{

alert('Hello World!'};

}

The rendered html contains :

<body id="ctl00_body" onload="TestJS();">

Can anyone give some help please?

Frustrated to no end over what should be a simple operation!!
Dec 6 '06 #2
Frank,

Have a look at the different "register" methods on this page.

http://msdn2.microsoft.com/en-us/lib...e_members.aspx

I hope this helps,

Cor

"Frank" <fk******@pfmail.comschreef in bericht
news:O5**************@TK2MSFTNGP03.phx.gbl...
Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css" rel="stylesheet"
type="text/css" />

<script language="JavaScript" src="common/js/global.js"
type="text/JavaScript"></script>

</head>

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

.

.

.

</body>

</html>

In the individual page code behind where I wish to call the js function,
it works if I use:

protected void Page_Load(object sender, EventArgs e)

{

try

{

string temp = "alert('Hello World!');";

HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");

body.Attributes.Add("onload", temp);

}

catch (NullReferenceException x)

{

Response.Write(x);

}

}

But if I try to call a function from the js file like so:

protected void Page_Load(object sender, EventArgs e)

{

try

{

string temp = "TestJS();";

HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");

body.Attributes.Add("onload", temp);

}

catch (NullReferenceException x)

{

Response.Write(x);

}

}

It fails. I don't get an ASP.NET exception, but rather a JavaScript error
which states it is expecting an object, but I can't figure out what object
it means. My js file looks like this:

// JScript File

function TestJS()

{

alert('Hello World!'};

}

The rendered html contains :

<body id="ctl00_body" onload="TestJS();">

Can anyone give some help please?

Frustrated to no end over what should be a simple operation!!

Dec 7 '06 #3
Thanks for the responses so far everyone.

Thanks for pointing out the bracket error... I corrected it and hoped for
success but no such luck.

I have seen a lot of code samples that use the
Page.ClientScript.RegisterClientScriptInclude(...) . you mentioned, but man,
what a pain. If I want to use the function on several different pages, I
then have to script it in all the code behind pages and then I believe it
embedds the script in the resulting html which is ugly.

I simply wish to code the JS function once in the code behind and then
execute it from some individual pages within a site... there must be a way
to do this. I spent too much time on this already but the code I posted
seems the closest to what I am looking for, however as it is, it throws a JS
error and I am unsure how to resolve it. Thanks for any further effort or
consideration.
Frank
Dec 7 '06 #4
OK, I got it:
<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
public void Page_Load(Object sender, EventArgs e)
{
// Define the name, type and url of the client script on the page.
String csname = "ButtonClickScript";
String csurl = "~/script_include.js";
Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the include script exists already.
if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
{
cs.RegisterClientScriptInclude(cstype, csname,
ResolveClientUrl(csurl));
}

//for the body onload functionality
string temp = "displaymessage()";
HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");
body.Attributes.Add("onload", temp);

}
<html >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<input type="text"
id="Message"/>
<input type="button"
value="ClickMe"
onclick="DoClick()"/>
</div>
</form>
</body>
</html>

This example requires a JavaScript file named Script_include.js with the
following contents:
function DoClick() {Form1.Message.value='Text from include script.'}
function displaymessage()
{
alert('Hello World!');
}
*** add <body runat="server" id="body"to masterpage for the pageload
functionality
Dec 7 '06 #5

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

Similar topics

1
by: cheezebeetle | last post by:
ok, so I am having problems passing in an ASPX function into the Javascript in the codebehind page. I am simply using a confirm call which when they press "OK" they call this ASPX function, when...
1
by: Tom S | last post by:
I've used C# to register a javascript function in the PageLoad event of a web form page. My problem is that I can't seem to figure out how to call it. I've tried the following, all to no avail: ...
4
by: Nevyn Twyll | last post by:
I have a web form, with c# code-behind. I have a listbox on the form, bound to a dataset. I want to have 2 buttons/hyperlinks/etc. beside the listbox. When they are clicked, I want to launch a...
8
by: Frank | last post by:
Hi, I am working with VS.NET 2005 Ultimately, I wish to call a JavaScript function from a .js file
6
by: MayBoy | last post by:
Hi There I am trying to use the Goto method of the Word ActiveX object. I am trying to open a document and go to a named bookmark. If I use this code in VB it works, so I'm sure the approach is...
11
by: srini | last post by:
Frens, I am trying to get screen resolution of the client machine using javascript and use those values in my project. I looked at some of the examples given in this group, i tried to implement...
5
by: =?Utf-8?B?TWF0Y29u?= | last post by:
Hello, I have a asp.net 2.0 (vb.net) page, which uses a master page, and there is a javascript function (myfunction) defined in the <headof that master page. Is there a way to run the function...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.