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.

4
  • What have you tried? What have you searched for? Commented Sep 13, 2012 at 11:13
  • Can you be a bit more specific? What's the 'open graph' tag? Why do you need to add it? Commented Sep 13, 2012 at 11:13
  • i want to add the open graph type tag <og:type..../> Commented Sep 13, 2012 at 11:16
  • 1
    Well, you can't update open graph properties once the page loads cause facebook requests it on page load. You should refer to this thread Commented Sep 13, 2012 at 11:22

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

well i want to add the code in between the <head></head> tags, is there a way to do that?
Yes, var head= document.getElementsByTagName('head')[0]; var script= document.createElement('script'); script.type= 'text/javascript'; script.src= '<somescript>.js'; head.appendChild(script); However, I doubt this will help adding og tags
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>.

1 Comment

does insterbefore works on code above </head> tag? api.jquery.com/insertBefore from here i can see that it only works for tags in between <body> not above that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.