Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Cleveland, Ohio
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need Help Styling Common Content Made with JavaScript

    Hi, I'm not good with manipulating JavaScript for my needs and I am trying to use a JavaScript file to implement my common content in to my web page, in this case it is a news article I want to display across all of my web pages. The problem is that I want to style it and use html attributes in side of the document.write and that is apparently messing it up. Would someone be able to show me how to fix this problem or fix the code so I can get the common content to display correctly with styling from CSS. If possible I would like to keep the CSS in a separate file and put a link to it in the JavaScript file and use the class tags, but I do not know how far we can go with JavaScript.
    Thanks for helping. Here are links to the webpage where I would like the code to show up. Below is the JavaScript that is currently in my .js file.

    http://www.nasawear.com/Home.html (The news article should display in the right column.)


    Code:
    document.write("<link rel="stylesheet" type="text/css" href="css/mainpage.css" />");
    document.write("<h3 class="section">");
    document.write("New NASA News");
    document.write("</h3>"); 
    document.write("
    <div class="picbox">
    <a target="_blank" href="image/superhotplanet-full.jpg">
    <img class="white" width="335" height="268" src="image/superhotplanet-full.jpg" alt="NASA Finds Super-Hot Planet with Unique Comet-Like Tail" /> Full Size</a>
    <p class="imageandstuff">NASA Finds Super-Hot Planet with Unique Comet-Like Tail</p>
    <p class="dis">
    Astronomers using NASA's Hubble Space Telescope have confirmed the existence of a baked object that could be called a "cometary planet." The gas giant planet, named HD 209458b, is orbiting so close to its star that its heated atmosphere is escaping into space.
    <br/>
    <br/>
    Observations taken with Hubble's Cosmic Origins Spectrograph (COS) suggest powerful stellar winds are sweeping the cast-off atmospheric material behind the scorched planet and shaping it into a comet-like tail.
    <br/>
    Illustration Credit: NASA, ESA, and G. Bacon (STScI)
    </p>
    </div> ");
    Last edited by DeMaster; 07-18-2010 at 06:45 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,951
    Thanks
    9
    Thanked 724 Times in 718 Posts
    Your javascript isn't working because it isn't formatted correctly - your quotation marks aren't consistent. For example, in:

    Code:
    document.write("<h3 class="section">");
    js sees the quotes in class="section as the end of the text to write, then finds more text after it and fails. This example should be:

    Code:
    document.write('<h3 class="section">');
    or:

    Code:
    document.write("<h3 class='section'>");
    Having said all that, it sounds like you are just trying to include a single bit of code on multiple pages, in which case a php include might be a better bet - and doesn't rely on your users having javascript enabled. Have a look here for some info.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •