1

I'm putting a javascript on a website, I also need to add an open graph tag above the tag of the page. Is there a way to add the code there using document.write or any other javascript method rather than asking the user to add it by himself which is not easy for a person without html knowledge.

Thanks.

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
rksh
  • 3,920
  • 10
  • 49
  • 68

2 Answers2

1

This is the example code to add a paragraph before an element with ID someID:

   var writeinTo = document.getElementsByID('someID');
   var para = document.createElement('p');
   writeinTo.parentNode.insertBefore(para, writeinTo);
Rob W
  • 341,306
  • 83
  • 791
  • 678
automaticAllDramatic
  • 2,025
  • 1
  • 21
  • 25
  • well i want to add the code in between the tags, is there a way to do that? – rksh Sep 13 '12 at 11:21
  • Yes, var head= document.getElementsByTagName('head')[0]; var script= document.createElement('script'); script.type= 'text/javascript'; script.src= '.js'; head.appendChild(script); However, I doubt this will help adding og tags – automaticAllDramatic Sep 13 '12 at 11:24
0

Take a look at insertBefore. If you're using jQuery, you can do it like this:

$('<meta name="foo" content="bar">').insertBefore('head');

Updated example because I thought the OP meant the <header> element, not <head>.

James
  • 5,137
  • 5
  • 40
  • 80
  • does insterbefore works on code above tag? http://api.jquery.com/insertBefore/ from here i can see that it only works for tags in between not above that – rksh Sep 13 '12 at 11:19