1

I use a hosted CMS that has a bug that won't allow me to insert content into the <head>.

I thought about using Javascript to insert the code needed into the head but will that even work? If a page is read <head> then <body> will it even matter to put the code into the head using javascript if the head has already been read or do I mis-read how the browsers work?

If you can put content into the head of a page and it will work with all major browsers what is the javascript to do that? (I am a novice when it comes to javascript.)

Note: I use a hosted CMS and do not have access to any server side scripting.

EDIT:

I need to add CSS and META tags.

The CSS would be something like this:

<style type="text/css">
 .slideshow img { display: none }
 .slideshow img.first { display: block }
</style>

The META could look like this:

<meta property="og:title" content="Title Text" />
<meta property="og:type" content="article" />
<meta property="og:url" content="URL Path" />
<meta property="og:image" content="IMG Path" />
<meta property="og:image:type" content="image/jpeg" />
<meta property="og:image:width" content="620" />
<meta property="og:image:height" content="541" />
<meta property="og:site_name" content="My Site" />

Those are examples but similar to what I will be adding.

7
  • 3
    What elements do you need to add to the head? CSS? JavaScript? Title tag? Meta tags? Commented Jul 22, 2012 at 1:34
  • @JCOC611 who would use JS to add JS? Commented Jul 22, 2012 at 1:35
  • 2
    @ColeJohnson - Many people. Most of the JavaScript API will do that. Commented Jul 22, 2012 at 1:35
  • 1
    @Derek damn you, beated me by 2 seconds. +1 Commented Jul 22, 2012 at 1:35
  • @JCOC611 - I updated question to reflect what I would be adding, mainly META and CSS elements. Commented Jul 22, 2012 at 1:38

2 Answers 2

1

For a CSS script:

var ele = document.createElement("style");
ele.type = "text/css";
ele.innerHTML = "CSS CODE"; // Replace with CSS code.
document.head.appendChild(ele);

For the Meta Tag (although I doubt it would work, since the software that looks at Meta Tags usually doesn't execute JavaScript code):

var ele = document.createElement("meta");
ele.property = "propertyName"; //Replace with property name.
ele.content = "CSS CODE"; // Replace with content.
document.head.appendChild(ele);
Sign up to request clarification or add additional context in comments.

4 Comments

I believe you may be right about the meta tags. I tried the method for CSS and it worked in FF and IE9 but breaks in IE 8. Thoughts?
Try it with document.body.appendChild(ele); (body instead of head), and see if it works.
Yeah, but it probably works too! Anyway, another thing you might want to try is changing your DOCTYPE to trigger IE8 in Compatibility mode.
I added IE Conditional Comments for IE8 and below (inside the body tag.) For all other browsers I am using the JS above. Thanks!
1
var ele = document.createElement("script");  //Tag name
ele.src = "your_link.js";                    //URL
document.head.appendChild(ele);              //Insert into <head>
                                             //Done.

3 Comments

Thanks, I will give this a try and see what happens. One more question if I want to use CSS like my question (I updated it since your answer => ) How would I add that?
@Lynda - Take a look at JCOC611's answer, it answers all your questions. ;) By the way, you should use jQuery, it is super-convenience in adding elements and manipulating the DOM.
I do use jQuery on my site and am fully open to jQuery solutions. =>

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.