pulling a var from a js file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fritz

    #1

    pulling a var from a js file

    Hi,
    I am not doing too well here.

    In the head I have:
    <link type="text/css" rel="stylesheet " href="myfirst.c ss">
    <script src="myfirst.js " type="text/javascript"></script>

    I have the following in 'myfirst.js' file:
    var writastring ="<table width="250" border="2"><tr>
    <td>Here is my first script text</td></tr></table>"

    I have the following in the myfirst.css:
    #ontheleft{posi tion:absolute; left:10px; top:50px; }

    In the body of the HTML file directly below the body tag is this:
    <div id="ontheleft" >
    <script language="javas cript">
    document.write( writastring)
    </script>
    </div>

    My problem is that it writes.......Fa nfare........
    exactly nothing.. nada.

    And yes they are all in the same path.
    Even if I change to this: var writastring="OK ! Write this"
    nothing is written. Even leaving out the Div tags nothing
    changes.

    The purpose of this exercise is to assemble content in the
    JS file and then write it in any number of pages.

    What have I done wrong? Any help would really be appreciated.


    f.
  • Jan Ebbe Jensen

    #2
    Re: pulling a var from a js file

    > var writastring ="<table width="250" border="2"><tr>[color=blue]
    > <td>Here is my first script text</td></tr></table>"[/color]
    [color=blue]
    >[/color]

    Try

    var writastring ="<table width=\"250\" border=\"2\"><t r>

    <td>Here is my first script text</td></tr></table>"

    Maybe it would help.

    Jan


    Comment

    • fritz

      #3
      Re: pulling a var from a js file

      Jan Ebbe Jensen wrote:[color=blue][color=green]
      >>var writastring ="<table width="250" border="2"><tr>
      >><td>Here is my first script text</td></tr></table>"[/color]
      >
      >
      >
      > Try
      >
      > var writastring ="<table width=\"250\" border=\"2\"><t r>
      >
      > <td>Here is my first script text</td></tr></table>"
      >
      > Maybe it would help.
      >
      > Jan
      >
      >[/color]
      I changed it as you suggested and can't believe I made the bonehead
      error of having multiple quote marks!
      I changed it to this in the js file:

      var writastring ="<table width='250' border='2'><tr> <td>Here is my first
      script text</td></tr></table>"

      Unfortunately it doesn't run. Now then, if I define this variable
      in the html file (either in the head or body) and then run it,
      it works fine. The problem is when the content is defined in the
      js file. As I said before, the idea is to assemble the content for
      this particular division somewhere where I can have one copy of it
      and then paste it into each page as it loads, making it easy to
      change for the entire site.
      I must be doing something very basic - wrong.
      Thanks for the help!

      f.

      Comment

      • Jan Ebbe Jensen

        #4
        Re: pulling a var from a js file

        It should work. It works fine at my computer. Try this code:

        test.html - file

        <html>
        <head>
        <link type="text/css" rel="stylesheet " href="myfirst.c ss">
        <script type="text/javascript" src="myfirst.js "></script>
        </head>
        <body>
        <div id="ontheleft" >
        <script language="javas cript">
        document.write( writastring);
        </script>
        </div>
        </body>
        </head>

        myfirst.js - file

        var writastring ="<table width=\"250\" border=\"2\"><t r><td>Here is my first
        script text</td></tr></table>";

        myfirst.css - file

        #ontheleft {
        position:absolu te; left:10px; top:50px;
        }

        Save the files in the same directory.

        Jan


        Comment

        • Fritz

          #5
          Re: pulling a var from a js file

          Jan Ebbe Jensen wrote:[color=blue]
          > It should work. It works fine at my computer. Try this code:
          >
          > test.html - file
          >
          > <html>
          > <head>
          > <link type="text/css" rel="stylesheet " href="myfirst.c ss">
          > <script type="text/javascript" src="myfirst.js "></script>
          > </head>
          > <body>
          > <div id="ontheleft" >
          > <script language="javas cript">
          > document.write( writastring);
          > </script>
          > </div>
          > </body>
          > </head>
          >
          > myfirst.js - file
          >
          > var writastring ="<table width=\"250\" border=\"2\"><t r><td>Here is my first
          > script text</td></tr></table>";
          >
          > myfirst.css - file
          >
          > #ontheleft {
          > position:absolu te; left:10px; top:50px;
          > }
          >
          > Save the files in the same directory.
          >
          > Jan
          >
          >[/color]
          Thanks Jan! Once I used the proper escape on the string variable
          and then found several cr/lf that made the code unreadable, all
          is well. I couldn't have done it without the help.

          Thank you
          Thank you

          f.

          Comment

          • Fred Oz

            #6
            Re: pulling a var from a js file

            fritz wrote:
            [...][color=blue]
            > Unfortunately it doesn't run. Now then, if I define this variable
            > in the html file (either in the head or body) and then run it,
            > it works fine. The problem is when the content is defined in the
            > js file. As I said before, the idea is to assemble the content for
            > this particular division somewhere where I can have one copy of it
            > and then paste it into each page as it loads, making it easy to
            > change for the entire site.[/color]

            Remember that anyone with JavaScript disabled or a non-JavaScript
            browser will not see your dynamic content.

            The type of script that you are suggesting is often run at the server
            to ensure that what is sent to the user is HTML and has no
            client-side dependencies outside support for stated the DOCTYPE.

            [...]

            --
            Fred

            Comment

            • fritz

              #7
              Re: pulling a var from a js file

              Fred Oz wrote:
              [color=blue]
              > Remember that anyone with JavaScript disabled or a non-JavaScript
              > browser will not see your dynamic content.
              >
              > The type of script that you are suggesting is often run at the server
              > to ensure that what is sent to the user is HTML and has no
              > client-side dependencies outside support for stated the DOCTYPE.
              >
              > [...]
              >[/color]

              Thank you Fred!

              Unfortunately I have absolutely no idea how I would even start to
              do what you mentioned. I am just beginning the javascript world and have
              no experience with the server side at all.

              I was planning to mention to visitors on the opening page that
              javascript and a modern browser were neccessary.

              Perhaps someday I will be able to understand what you call the
              server side. I assume you mean something to do with CGI? But thanks for
              the comments.

              f.

              Comment

              Working...