Setting <body> tag's arguments programmatically

D

davidemazza82

Hi all,

I need to set the attributes onload and onresize of the <body> tag
in a HTML file via javascript code.

I want to obtain a programmatic equivalent of the following HTML line:

<body onload="onLoad();" onresize="onResize();">

When I try to inspect the content of the attributes with the following
Javascript code:

var body_elm = document.getElementsByTagName('body').item(0);
document.writeln(body_elm.getAttribute('onload'));
document.writeln(body_elm.getAttribute('onresize'));

I got:

function anonymous() { onLoad(); } function
anonymous() { onResize(); }

But if I substitute the previous <body> tag with one without
arguments:

<body>

and try to set the attribute values programmatically:

var html_doc = document.getElementsByTagName('body').item(0);
html_doc.setAttribute('onload', 'Timeline:eek:nLoad();' );
html_doc.setAttribute('onresize', 'Timeline:eek:nResize();');

then, inspecting the attribute values, I got:

onLoad(); onResize();


How can I obtain the same values for the attributes as in the first
case:

function anonymous() { onLoad(); } function
anonymous() { onResize(); }

by setting them programmatically with the setAttribute(attrname,
attrvalue) method?

Tnx, Davide
 
C

Captain Paralytic

Hi all,

I need to set the attributes onload and onresize of the <body> tag
in a HTML file via javascript code.

I want to obtain a programmatic equivalent of the following HTML line:

<body onload="onLoad();" onresize="onResize();">

When I try to inspect the content of the attributes with the following
Javascript code:

var body_elm = document.getElementsByTagName('body').item(0);
document.writeln(body_elm.getAttribute('onload'));
document.writeln(body_elm.getAttribute('onresize'));

I got:

function anonymous() { onLoad(); } function
anonymous() { onResize(); }

But if I substitute the previous <body> tag with one without
arguments:

<body>

and try to set the attribute values programmatically:

var html_doc = document.getElementsByTagName('body').item(0);
html_doc.setAttribute('onload', 'Timeline:eek:nLoad();' );
html_doc.setAttribute('onresize', 'Timeline:eek:nResize();');

then, inspecting the attribute values, I got:

onLoad(); onResize();

How can I obtain the same values for the attributes as in the first
case:

function anonymous() { onLoad(); } function
anonymous() { onResize(); }

by setting them programmatically with the setAttribute(attrname,
attrvalue) method?

Tnx, Davide

I'm trying to get my head around this one?

onLoad() fires when the page loads.
Until the page has loaded, there is no body tag.
Where is your program going to exist to put the onLoad into the body
tag, when it doesn't yet exist?
 
P

pr

How can I obtain the same values for the attributes as in the first
case:

function anonymous() { onLoad(); } function
anonymous() { onResize(); }

by setting them programmatically with the setAttribute(attrname,
attrvalue) method?

Assuming you don't simply want the string 'function anonymous() {
onResize()... ' in the onload attribute of the body element, you must
mean you want to swap the handler for an event that has already been and
gone with a function named 'anonymous'.

Why?
 
M

machineghost

I may be misunderstanding, but I think you don't really want to change
your body's attributes at all. What you really want is to hook up two
new event handlers to the body element (although it would probably
make more sense to hook up the onload one to the window instead). You
can do this one of three ways:
1. Use the cross-browser method:
body_elm.onresize = yourFunction
2. Use a combination of either the DOM or the IE methods (depending
on the browser) for hooking up events. I'm too lazy to detail this,
but a quick search for "javascript event hookup" or something similar
should help.
3. Use one of the many excellent javascript libraries out there. For
instance, here's a Mochikit version:
connect(body_elm, "onresize", yourFunction);

Hope that helps,
Jeremy

(e-mail address removed) wrote:

[...]


How can I obtain the same values for the attributes as in the first
case:
function anonymous() { onLoad(); } function
anonymous() { onResize(); }
by setting them programmatically with the setAttribute(attrname,
attrvalue) method?

Assuming you don't simply want the string 'function anonymous() {
onResize()... ' in the onload attribute of the body element, you must
mean you want to swap the handler for an event that has already been and
gone with a function named 'anonymous'.

Why?
 
D

David Mark

Hi all,

I need to set the attributes onload and onresize of the <body> tag
in a HTML file via javascript code.

No you don't.
I want to obtain a programmatic equivalent of the following HTML line:

<body onload="onLoad();" onresize="onResize();">

The what of the what?
When I try to inspect the content of the attributes with the following
Javascript code:

var body_elm = document.getElementsByTagName('body').item(0);
document.writeln(body_elm.getAttribute('onload'));
document.writeln(body_elm.getAttribute('onresize'));

I got:

function anonymous() { onLoad(); } function
anonymous() { onResize(); }

In IE obviously. That's because IE's programmers have never figured
out what getAttribute is supposed to do.
But if I substitute the previous <body> tag with one without
arguments:

<body>

and try to set the attribute values programmatically:

var html_doc = document.getElementsByTagName('body').item(0);
html_doc.setAttribute('onload', 'Timeline:eek:nLoad();' );
html_doc.setAttribute('onresize', 'Timeline:eek:nResize();');

then, inspecting the attribute values, I got:

onLoad(); onResize();

In IE these are just strings stored in properties of the body element
object, so don't expect these functions to be called. IE botches
setAttribute too.
How can I obtain the same values for the attributes as in the first
case:

function anonymous() { onLoad(); } function
anonymous() { onResize(); }

by setting them programmatically with the setAttribute(attrname,
attrvalue) method?

In answer to your question:

html_doc.onload = function() { onLoad(); };

But I assume that you asked the wrong question and really want this
answer:

html_doc.onload = onLoad;

And why call a variable that references the body element "html_doc?"

Anyway, I would do this:

this.onload = onLoad;

Never mind the body element or what getAttribute says about it.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top