pstein Posted May 16, 2017 Share Posted May 16, 2017 (edited) 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 May 16, 2017 by pstein Link to comment Share on other sites More sharing options...
davej Posted May 16, 2017 Share Posted May 16, 2017 As the last item at the bottom of the body add your code or; <script>document.write('<br>'+document.URL);</script> Link to comment Share on other sites More sharing options...
dsonesuk Posted May 16, 2017 Share Posted May 16, 2017 (edited) 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 May 16, 2017 by dsonesuk Link to comment Share on other sites More sharing options...
dsonesuk Posted May 16, 2017 Share Posted May 16, 2017 (edited) 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 May 16, 2017 by dsonesuk Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now