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

Run JScript from .js file?

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.GetTy pe(),"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?
Dec 7 '06 #1
7 4960
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.

--
PP
Dec 7 '06 #2
Hi,

Frank wrote:
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.GetTy pe(),"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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Dec 7 '06 #3
Hi,

Przemek Ptasznik wrote:
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();
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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Dec 7 '06 #4
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("glo bal.js",
http://localhost:3929/Statements/sta...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("");

}

}


"Laurent Bugnion" <ga*********@bluewin.chwrote in message
news:OH****************@TK2MSFTNGP03.phx.gbl...
Hi,

Frank wrote:
>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.GetT ype(),"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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Dec 7 '06 #5
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>.
Dec 7 '06 #6
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
Dec 7 '06 #7
Laurent Bugnion napisał(a):
>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.
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:)
--
PP
Dec 7 '06 #8

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

Similar topics

20
by: Harag | last post by:
Hi All. I'm stating out doing some web developing. I was wondering which of the server side languages should I concentrate on and learn. I Know CSS, HTML, T-SQL I can look at the client...
4
by: Harag | last post by:
Hi All I currently thinking of converting from my little knowledge of VBscript to jScript ASP. With this in mind I'm looking at my current code to see how it will convert over to Jscript. ...
1
by: TopDawg044 | last post by:
I continue to lose Javascript functionality in any browser I use under WinXPPro...(IE6, Moz 1.4, NS 7.1...)it is now at the point where I'm unable to access my school sites which use jscript and...
7
by: Dr John Stockton | last post by:
The following DOS prompt command line, in a Win98 DOS box, cscript //nologo ~tmp.js executes file ~tmp.js as javascript/Jscript. The following batch file, similarly run, executes the same....
1
by: joe | last post by:
Hello, I have some code written in C that I want to transfer over to JScript so I can run within my project to "burn" my DSP code into EEPROM(Non-volatile RAM). I want to generate an .ldr file (a...
0
by: tvin | last post by:
Hello all, I have a problem to use jscript.js file in aspx page: I wrote jscript file in Notepad and I compiled this jscript file,after compiled and .exe in command prompt,i see that the...
2
by: Jer425 | last post by:
Hello, I'm coding in VB and trying to include a jscript file. This will not work because you can only use one language in asp.net. Is there a way that I can use the jscript without having to...
3
by: bowser | last post by:
Hello, I have a problem of communication between JScript and C#. I must say that I'm new to both. In a JS file I have to call a C# function. In particular I have: public function Eval(expr :...
1
by: Andrew Wan | last post by:
How can VBScript code access JScript code variables in the same ASP page? <SCRIPT LANGAUGE="VBScript"> Dim a a = 10 </SCRIPT> <SCRIPT LANGUAGE="JScript"> Response.Write(a); </SCRIPT>
15
by: zz12 | last post by:
Hello, would anyone be able to confirm that 'jscript.dll' is a necessary file for an .asp page on IIS 5.0 to use the <script language="JavaScipt" runat="SERVER"code? It looks like the code in this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.