0

I'm trying to use JavaScript to include a footer on several webpages, so if I want to change the footer, I only have to change it in one place. PHP is not available on this server and neither are server side inserts (SSI), but Perl, Python, and Tcl are available. I have been trying with document.getElementsByTagName('footer').innerHTML = "text"; but it doesn't produce text. I copied this code from dev.mozilla, and it tells me how many tags I have:

var footer = document.getElementsByTagName('footer');
var num = footer.length;
console.log('There is ' + num + ' footer in this document');

So, I don't know what's wrong with the innerHTML script. I also tried with paragraph tags and got the same results in both cases.

Kete
  • 1
  • 2

3 Answers3

0

I reccoment using textContent instead. Se why here.

To see how it works, paste the following into your browser console while you're on StackOverflow and hit enter.

document.querySelector('.site-footer').textContent = 'Custom footer content.'

note: use querySelector with a class instead of getElementByTagName

Cheers!

Raz Chiriac
  • 406
  • 2
  • 7
0

Before asking this question, I had searched for Python includes without any luck, so I stopped there, but after asking this question, I thought that I should search for Perl/Ruby includes. Today, I found out that I can use the Perl use function, so I could study that and try to implement it although I am completely new to Perl. Ruby also appears capable, perhaps even more. I have no experience with Ruby either, but maybe I should start there.

Kete
  • 1
  • 2
0

I just figured out that getElementsByTagName() results in an array, so I have to refer to the footer's index with [0]:

var footerTags = document.getElementsByTagName('footer');
footerTags[0].innerHTML = "test";
Kete
  • 1
  • 2