Jump to content

document.getElementsByTagName('body'); Not working?


pstein

Recommended Posts

I want to insert the current URL at the bottom of a web page (before printing it).

Therefore I  coded:

var address = document.URL;
var pane = document.getElementsByTagName('body');
pane.innerHTML = pane.innerHTML + "<br/>" + address;

But this doesn't work.

The URL is NOT shown.

How else can I achieve this?

Peter

 

Edited by pstein
Link to comment
Share on other sites

I know it sounds silly because there is only one body element, but

var pane = document.getElementsByTagName('body');

produces a list of body elements found, while

var pane = document.getElementsByTagName('body')[0];

Targets the first body element in that list, where index always start from 0.

Imagine you had several divs, you wouldn't use just

var pane = document.getElementsByTagName('div'); you would use index of the div you wish to target, or loop through each to manipulate.

Edited by dsonesuk
Link to comment
Share on other sites

OR you can go the other way apply index ref to variable pane

            var pane = document.getElementsByTagName('body');
            pane[0].innerHTML = pane[0].innerHTML + "<br/>" + address;

I don't even know why davej even suggested using document.write()? with all problems that come with it, he has even advised against it if I remember rightly.

Anyway, another alternative is

            var pane = document.body;
            pane.innerHTML += "<br/>" + address;

 

Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...