Client-side includes with JavaScript

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

    Client-side includes with JavaScript

    I'm trying to simulate server-side includes on the client-side by
    using JavaScript. My main problem is this: is there a way to somehow
    pass a variable into a directive like this:

    <SCRIPT LANGUAGE="JavaS cript" type="text/javascript"
    src="$myPageVar $"></SCRIPT>


    ....where $myPageVar$ is a variable I've declared, and somehow,
    magically, its value gets put into this directive? The only way I can
    get this to work is if I hard-code a value for $myPageVar$ in the
    following way:

    <SCRIPT LANGUAGE="JavaS cript" type="text/javascript"
    src="homeInc.js "></SCRIPT>


    ....which is of limited use. Is there a way for me to use variables in
    such a directive? Or is there a better way to be doing client-side
    includes?

    Thanks.
  • kaeli

    #2
    Re: Client-side includes with JavaScript

    In article <85e06fdf.03081 41215.42db893@p osting.google.c om>,
    greg@ZeroG.com enlightened us with...[color=blue]
    > I'm trying to simulate server-side includes on the client-side by
    > using JavaScript. My main problem is this: is there a way to somehow
    > pass a variable into a directive like this:
    >
    > <SCRIPT LANGUAGE="JavaS cript" type="text/javascript"
    > src="$myPageVar $"></SCRIPT>
    >
    >[/color]

    This worked in IE...(put your file name where mine is for myVar)

    <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title> New Document </title>
    <script>
    myVar = "jsCaptureEnter Key.js";
    document.write( "<s"+"cript src='"+myVar+"' ></s"+"cript>") ;
    </script>
    </head>

    <body>
    hi
    </body>
    </html>


    -------------------------------------------------
    ~kaeli~
    Why do people who know the least know it the loudest?
    If that cell phone was up your a$$, maybe you could
    drive a little better!


    -------------------------------------------------

    Comment

    • Douglas Crockford

      #3
      Re: Client-side includes with JavaScript

      > <html>[color=blue]
      > <head>
      > <title> New Document </title>
      > <script>
      > myVar = "jsCaptureEnter Key.js";
      > document.write( "<s"+"cript src='"+myVar+"' ></s"+"cript>") ;
      > </script>
      > </head>[/color]

      The trick with writing script tags is that browsers get confused by scripts
      containing "</" in theory and "</script>" in practice. The line above fails the
      in theory case. Try this instead:

      document.write( '<script src="' + myVar + '"><\/script>');

      Putting spaces around the plus signs make it much easier to read.




      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Client-side includes with JavaScript

        kaeli <infinite.possi bilities@NOSPAM att.net> writes:
        [color=blue]
        > In article <85e06fdf.03081 41215.42db893@p osting.google.c om>,
        > greg@ZeroG.com enlightened us with...[/color]
        [color=blue][color=green]
        > > <SCRIPT LANGUAGE="JavaS cript" type="text/javascript"
        > > src="$myPageVar $"></SCRIPT>[/color]
        >
        > This worked in IE...(put your file name where mine is for myVar)[/color]

        Yes, the trick is to document.write the script tag with your
        variables in place. (scripts of more scripts!).
        [color=blue]
        > <script>[/color]

        <script type="text/javascript">

        Just for being pedantic, the type attribute is mandatory in HTML 4,
        and you did add the DOCTYPE (a bad DOCTYPE that puts browsers into
        quirks mode, though, but that's another rant).
        [color=blue]
        > myVar = "jsCaptureEnter Key.js";
        > document.write( "<s"+"cript src='"+myVar+"' ></s"+"cript>") ;[/color]

        Just write it as
        document.write( "<script src='"+myVar+"' ><\/script>");

        You don't need to split "script". The one thing that a script is not
        allowed to contain (according to specification) is the sequence "</".
        In practice, browsers allow that, and only ends the script tag at
        "</script>". In either case, just one backslash is sufficient.

        /L 'Just needed to say that!'
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • kaeli

          #5
          Re: Client-side includes with JavaScript

          In article <wudgym1i.fsf@h otpop.com>, lrn@hotpop.com enlightened us
          with...[color=blue]
          >
          > Just for being pedantic, the type attribute is mandatory in HTML 4,
          > and you did add the DOCTYPE (a bad DOCTYPE that puts browsers into
          > quirks mode, though, but that's another rant).
          >[/color]

          Please explain.
          My html editor sticks that in, and I can change it, so if it's a bad
          one, I'd like to know about it.



          Thanks for the tips, both of you who replied.

          -------------------------------------------------
          ~kaeli~
          Why do people who know the least know it the loudest?
          If that cell phone was up your a$$, maybe you could
          drive a little better!


          -------------------------------------------------

          Comment

          • Dr John Stockton

            #6
            Re: Client-side includes with JavaScript

            JRS: In article <wudgym1i.fsf@h otpop.com>, seen in
            news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
            posted at Thu, 14 Aug 2003 23:19:37 :-[color=blue]
            >
            >You don't need to split "script". The one thing that a script is not
            >allowed to contain (according to specification) is the sequence "</".
            >In practice, browsers allow that, and only ends the script tag at
            >"</script>". In either case, just one backslash is sufficient.[/color]

            However, W3's TIDY, in checking mode, will give

            Warning: '<' + '/' + letter not allowed here

            for </bbb> in a test page containing

            <script>
            S = '</bbb>'
            </script>

            and it is useful to get a warning-free test even if the tester may be
            over-strict.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            • Douglas Crockford

              #7
              Re: Client-side includes with JavaScript

              > >You don't need to split "script". The one thing that a script is not[color=blue][color=green]
              > >allowed to contain (according to specification) is the sequence "</".
              > >In practice, browsers allow that, and only ends the script tag at
              > >"</script>". In either case, just one backslash is sufficient.[/color]
              >
              > However, W3's TIDY, in checking mode, will give
              >
              > Warning: '<' + '/' + letter not allowed here
              >
              > for </bbb> in a test page containing
              >
              > <script>
              > S = '</bbb>'
              > </script>
              >
              > and it is useful to get a warning-free test even if the tester may be
              > over-strict.[/color]

              jslint also checks for this.



              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: Client-side includes with JavaScript

                kaeli <infinite.possi bilities@NOSPAM att.net> writes:

                [DOCTYPE][color=blue]
                > Please explain.
                > My html editor sticks that in, and I can change it, so if it's a bad
                > one, I'd like to know about it.[/color]

                Opera has an explanation of the DOCTYPE switching
                <URL:http://www.opera.com/docs/specs/doctype/>
                It behaves the same as IE and almost the same as Mozilla, and there
                are also links to Microsoft and Mozilla pages on the same subject.

                The short summary is that some DOCTYPEs puts browsers into
                backwards compatability mode, where they emulate the bugs of IE4
                (because a lot of pages were written for that browser and fails
                completely in a purely standards compliant browser). New pages
                should not be created for to backwards compatible mode, but to
                standards mode.

                Your DOCTYPE was for HTML 4.01 Transitional and with no URL, which
                triggers quirks mode in all three browsers.

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • kaeli

                  #9
                  Re: Client-side includes with JavaScript

                  In article <vfsylgka.fsf@h otpop.com>, lrn@hotpop.com enlightened us
                  with...[color=blue]
                  >
                  > Your DOCTYPE was for HTML 4.01 Transitional and with no URL, which
                  > triggers quirks mode in all three browsers.
                  >[/color]

                  That is VERY useful to know.

                  Is it better to omit the doctype or should I use the proper one?


                  -------------------------------------------------
                  ~kaeli~
                  Why do people who know the least know it the loudest?
                  If that cell phone was up your a$$, maybe you could
                  drive a little better!


                  -------------------------------------------------

                  Comment

                  • Lasse Reichstein Nielsen

                    #10
                    Re: Client-side includes with JavaScript

                    kaeli <infinite.possi bilities@NOSPAM att.net> writes:
                    [color=blue]
                    > Is it better to omit the doctype or should I use the proper one?[/color]

                    Use a proper one. Omitting a doctype also triggers quirks mode.
                    /L
                    --
                    Lasse Reichstein Nielsen - lrn@hotpop.com
                    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                    'Faith without judgement merely degrades the spirit divine.'

                    Comment

                    • kaeli

                      #11
                      Re: Client-side includes with JavaScript

                      In article <oeynkog9.fsf@h otpop.com>, lrn@hotpop.com enlightened us
                      with...[color=blue]
                      > kaeli <infinite.possi bilities@NOSPAM att.net> writes:
                      >[color=green]
                      > > Is it better to omit the doctype or should I use the proper one?[/color]
                      >
                      > Use a proper one. Omitting a doctype also triggers quirks mode.
                      > /L
                      >[/color]

                      Excellent. Thanks for all the help!

                      --
                      -------------------------------------------------
                      ~kaeli~
                      Why do people who know the least know it the loudest?
                      If that cell phone was up your a$$, maybe you could
                      drive a little better!


                      -------------------------------------------------

                      Comment

                      Working...