Run JScript from .js file?

F

Frank

Hi,

I simply wish to code a JS function ONCE, in a .js file, include the .js
file in the Master page, and then in several but not all pages, I wish to
execute the function on pageload.

I don't want to use the
Page.ClientScript.RegisterStartupScript(this.GetType(),"displaymessage",
strScript, false);

method because this seemingly requires me to code the JScript on all the
pages (code behind) where I want to run the function, which is stupid.



I think what I want to use is something like :



Designate the body tag as :

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

and in the code behind for a particular page:

protected void Page_Load(object sender, EventArgs e)

{

try

{

string temp = "displaymessage();";


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

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

}

catch (NullReferenceException x)

{

Response.Write(x);

}

}



but this give a JS error where it says 'Object Required' where the body tag
is in the rendered html



It does work if I do:

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);

}

}



So what is wrong?
 
P

Przemek Ptasznik

Frank napisa³(a):
Hi,

I simply wish to code a JS function ONCE, in a .js file, include the .js
file in the Master page, and then in several but not all pages, I wish to
execute the function on pageload.

Just put into head element an script element pointing to .js file.
And then in js file, where your function is defined add line:

window.onload=your_function_name();

thats all.
 
L

Laurent Bugnion

Hi,
Hi,

I simply wish to code a JS function ONCE, in a .js file, include the .js
file in the Master page, and then in several but not all pages, I wish to
execute the function on pageload.

I don't want to use the
Page.ClientScript.RegisterStartupScript(this.GetType(),"displaymessage",
strScript, false);

method because this seemingly requires me to code the JScript on all the
pages (code behind) where I want to run the function, which is stupid.

You could derive your pages from a common base class, which is in charge
of including the desired JavaScript code.

However, if the method doesn't change, it's better to add it to an
external JS file, and include the JS file in your pages using

<script type="text/javascript" src="yourJsFile.js"></script>

You can either code this in the ASPX, or you can use the
RegisterClientScript method to do this.

Then have the method called onload of the body, like you do before.
I think what I want to use is something like :



Designate the body tag as :

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

and in the code behind for a particular page:

protected void Page_Load(object sender, EventArgs e)

{

try

{

string temp = "displaymessage();";


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

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

}

catch (NullReferenceException x)

{

Response.Write(x);

}

}



but this give a JS error where it says 'Object Required' where the body tag
is in the rendered html

Yes, that's because the method "displayMessage" is not defined anywhere.
Since functions are objects in JavaScript, the message is "Object
required" ;-)

HTH,
Laurent
 
L

Laurent Bugnion

Hi,

Przemek said:
Frank napisa³(a):

Just put into head element an script element pointing to .js file.
And then in js file, where your function is defined add line:

window.onload=your_function_name();

This is a common error, but a nasty one: You allocate the *result* of
the "your_function_name" function to the event handler. Unless the
result is a function, it's probably not what you want.

You want

window.onload = your_function_name;

In JavaScript, functions are objects.

HTH,
Laurent
 
F

Frank

Thanks for your responses so far.

I want to point out that I do have it defined in the js file like so:

// JScript File

function displaymessage()

{

alert("Hello World!")

}

and in the master page I have

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

<head runat="server">



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

</head>




However, if the method doesn't change, it's better to add it to an
external JS file, and include the JS file in your pages using

<script type="text/javascript" src="yourJsFile.js"></script>

You can either code this in the ASPX, or you can use the
RegisterClientScript method to do this.



I tried to follow your suggestion... Here is what I coded in the page load
but still to no avail... any suggestions?

protected void Page_Load(object sender, EventArgs e)

{

try

{

Page.ClientScript.RegisterClientScriptInclude("global.js",
http://localhost:3929/Statements/statements/common/js/global.js);

string temp = "displaymessage()";

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

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

}

catch (NullReferenceException x)

{

Response.Write(x);

}

finally

{

Response.Write("");

}

}
 
F

Frank

Thanks for your repsonse. I have tried this:

Created a file name global.js which contains:
// JScript File

function displaymessage()

{

alert("Hello World!")

}

Then in the Master page I did like so:

<head runat="server">



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

</head>

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



Then in one of the pages in the site I did like so:

protected void Page_Load(object sender, EventArgs e)

{

// Define the name, type and url of the client script on the page.

String csname = "PageLoadScript";

String csurl = "~/common/gjs/global.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));

}

string temp = "displaymessage()";

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

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

}



The page starts up but a JS error occurs (Object Expected) and point to the
body tag line... The rendered HTML starts like so:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Untitled Page
</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 id="ctl00_body" onload="displaymessage()">
<form name="aspnetForm" method="post" action="test.aspx"
id="aspnetForm">
<div>.
 
F

Frank

OK, I got it to work finally:

<%@ 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
 
P

Przemek Ptasznik

Laurent Bugnion napisa³(a):
This is a common error, but a nasty one: You allocate the *result* of
the "your_function_name" function to the event handler. Unless the
result is a function, it's probably not what you want.

Aaaaaaarghh!
You're 100% right:) It's ma mistake. Of course i mean providing only
function name without parenthesis, exactly as you show below.
window.onload = your_function_name;

In JavaScript, functions are objects.
Yes I know.

anyway, this solution is much easier than the few proposed in other posts.

Thanks for correcting:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top