简体   繁体   中英

Write code above <header> tag using Javascript

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.

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);

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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM