Click to See Complete Forum and Search --> : Changing HTML


geuis
03-02-2003, 11:30 AM
I am trying to figure out how to modify the HTML from a site after its been loaded.

For example, a page that loads has:
<body bgcolor="black" onload="doSomething()">
......
.....
</body>

What I cannot figure out is how to, say, remove the "onload=" from the body tag.
Some folks have said you can use something called DOM to do this, but the website netscape has on this is hard to follow.

If someone could actually show me some sample code, such as how to do it for this example I gave, then I can use that as a point of reference to really understand how to use it on the netscape site. Thanks.

Geuis

AdamBrill
03-02-2003, 12:35 PM
Well, you could change it like this:

window.onload=doSomethingElse;

Then, on the onload, it will run the doSomethingElse function. I guess I'm not exactly sure what the point is to changing the onload after it has alread loaded. :confused:

khalidali63
03-02-2003, 12:47 PM
There are 2 different approaches for this to work both on NS and IE.

first get an elements reference using DOM

var parent = document.getElementBysTagName("body")[0]

Above the tag names will return a NodeList array of all the elements by the name,and we know( I hope) the body tag is only one,so we can hard code the first element using array index [0].

now we have element reference "parent".

to add and attribute to this element use the setAttribute("attrib name","value") method.
Supose you wanted to change the bgcolor as well
parent.setAttribute("bgcolor","green");

.Now to add events or to make changes to events you will have to work a bit harder.
for NS
parent.addEventListener("mouseover",eventHandlerName,false)

for IE
parent.attachEvent("onmouseover",eventHandlerName);

where eventHandlerName is the function that has some processing info when onmouseover event is triggered.

Hope this helps

Cheers

Khalid

Nedals
03-02-2003, 12:51 PM
Are you trying to modify the HTML of a page from someone else's site. If that's the case, you can't !!

To expand a little on AdamBrill's comment. Once the page has loaded, the doSomething() function doesn't do anything so changing it or removing it, after the page has loaded, is pointless.