Re: JavaScript From PageLoad()?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Frank,

Also, if you do get a problem with the methods not being immediately
available once the page is loaded you can wrapper you call with a
SetTimeout function with a very low timeout value. There is a bug in
some browsers where it does need that very slight delay.

I had this very problem the other day with Firefox while it was working
fine in IE. The timeout delay made it work.

Brennan Stehling
http://brennan.offwhite.net/blog/


Frank wrote:
Thanks for the responses so far everyone.

rb, the reason I don't just add <body id="body" onload="TestJS();"> to the
masterpage html, is because this is a function I want to execute on only
certain pages. If I out it into the Masterpage html, it will execute on all
pages.

And Brennan, I have seen a lot of code samoles 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
themn 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


"rb" <rajko_at_nospam_sheermomentum.com> wrote in message
news:ePo05kfGHHA.4588@xxxxxxxxxxxxxxxxxxxxxxx

Thanks for the lecture but RegisterClientScriptInclude and UserControls
don't have much to do with Frank's question.

He works with master page and wants to use/call javascript function from
body.onload. He encountered a problem where by if he calls alert() from
onload everything seem to be fine. However, if he calls actual javascript
function he gets an error.

So, back to my "solution", since nothing seem to be dynamic, why not just
hardcode call to javascript from body.onload.

rb


"Brennan Stehling" <offwhite@xxxxxxxxx> wrote in message
news:1165462930.007093.84270@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
rb,

This technique generates those script tags. With ASP.NET you can build
a page with a series of User Controls which are fragments you can use
throughout the page. And a User Control does not have a HEAD tag at
all. It is like a Server-Side Include. You may have a User Control
represent a product display and have 6 products shown on the page. If
you had that control use a little Javascript you use this technique to
tell it to load the script just once by making sure it is not
registered before you do. Then your User Control can call methods in
that script files as needed.

For the sites I build with ASP.NET I take advantage of Master Pages
which act like a template for the whole website. The Master Page may
have User Controls for the navigation and footer areas. Then the Page
portion may use more User Controls.

I break things up into small User Controls as much as reasonable to
greatly simplify maintenance.

But if I do have a script I know I always wanted included for every
page I can still just place it in the Master Page header as a regular
script page.

Brennan Stehling
http://brennan.offwhite.net/blog/

rb wrote:
I don't get it. Why don't you just do it as usual:

<script language="JavaScript" src="common/js/global.js"
type="text/JavaScript"></script>
<snip>
<body id="body" onload="TestJS();">

rb


"Frank" <fkaesser@xxxxxxxxxx> wrote in message
news:u7E7k5YGHHA.1252@xxxxxxxxxxxxxxxxxxxxxxx
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="style***"
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!!





.


Quantcast