Executing Javascript generated by PERL

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

    Executing Javascript generated by PERL

    I am wanting to use javascript to select between different *.css files
    dependent on the user's browser. I am also wanting to generate the
    html document containing this javascript dynamically using PERL.

    So far, I have the javascript that does what I want to do, and it works
    fine as long as the the page was not generated by PERL.

    My problem is that when the javascript/html is generated by
    my PERL scripts the javascript never gets executed by the clients
    browser. Thus, no stylesheets are ever applied to the page.


    Some of my code samples:

    ############### ############### ############### ############
    #the relevant sections from my PERL script:

    sub header() {
    print "Content-type:text/html\n\n";
    print << "HEADER";
    <html>
    <head>
    <title>****</title>
    <script type="text/javascript" src="./cssSelect.js"
    lanaguage="java script"></script>
    </head>
    HEADER
    }

    #One Note: I have tried using the absolute path in the src tag. Still no
    #luck.
    #This goes generates rest of html page...

    ############### ############### ############### ###########
    #cssSelect.js

    var css = (navigator.user Agent.indexOf(" MSIE ") != -1 ? "./css1.css" :
    ../css2.css";
    document.write( "<link rel='stylesheet ' type='text/css' href='" + css +
    "' />");

    #As mentioned before, this works fine when not being generated by a PERL
    script.

    Getting javascript to execute after being generated by PERL seems to be
    a problem other people have run into over the years, but I haven't been able
    to find a solution that works in my case.

    Thanks in advance,
    NG
    --
    "The life of a repoman is always intense."
  • Randy Webb

    #2
    Re: Executing Javascript generated by PERL

    Nathan Gilbert said the following on 6/26/2006 5:33 PM:[color=blue]
    > I am wanting to use javascript to select between different *.css files
    > dependent on the user's browser.[/color]

    You can't as you have no way of reliably determining the browser.
    [color=blue]
    > I am also wanting to generate the html document containing this
    > javascript dynamically using PERL.[/color]

    OK, then do.
    [color=blue]
    > So far, I have the javascript that does what I want to do, and it works
    > fine as long as the the page was not generated by PERL.[/color]

    Then PERL is screwing it up somehow as the browser doesn't know, doesn't
    need to know, and doesn't care what generated the script.
    [color=blue]
    > My problem is that when the javascript/html is generated by
    > my PERL scripts the javascript never gets executed by the clients
    > browser. Thus, no stylesheets are ever applied to the page.[/color]

    If you want one set of stylesheets for IE and another for all other
    browsers, then use IE conditionals and not javascript for it.
    [color=blue]
    >
    > Some of my code samples:
    >
    > ############### ############### ############### ############
    > #the relevant sections from my PERL script:
    >
    > sub header() {
    > print "Content-type:text/html\n\n";
    > print << "HEADER";
    > <html>
    > <head>
    > <title>****</title>
    > <script type="text/javascript" src="./cssSelect.js"
    > lanaguage="java script"></script>[/color]

    lanaguage? but the language attribute is not needed.

    <snip>
    [color=blue]
    > var css = (navigator.user Agent.indexOf(" MSIE ") != -1 ? "./css1.css" :
    > ../css2.css";[/color]

    All three browsers that I use daily will pass the 'MSIE' test but only
    one is IE.
    [color=blue]
    > #As mentioned before, this works fine when not being generated by a PERL
    > script.[/color]

    Then why generate static content with PERL?

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
    Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

    Comment

    • Nathan Gilbert

      #3
      Re: Executing Javascript generated by PERL

      Randy Webb <HikksNotAtHome @aol.com> once pondered:[color=blue]
      > Nathan Gilbert said the following on 6/26/2006 5:33 PM:[color=green]
      >> I am wanting to use javascript to select between different *.css files
      >> dependent on the user's browser.[/color]
      >
      > If you want one set of stylesheets for IE and another for all other
      > browsers, then use IE conditionals and not javascript for it.
      >[/color]

      Thanks for the tip, I replaced the javascript with IE conditionals.
      Everything works now the way I intended.

      Is there any trick to getting PERL generated javascript to execute in
      the user's browser? Can someone point out a tutorial that explains how
      and when most browsers parse and execute javascript?

      Maybe if I can find out more about how browsers work, I can figure out
      why my original method didn't work, just for curiosity.

      Thanks in advance,
      NG
      --
      "The life of a repoman is always intense."

      Comment

      • David McNerney

        #4
        Re: Executing Javascript generated by PERL

        There's no problem using "Perl generated" JS; I do it constantly on
        various web browsers, with no Perl related problems whatever. Sometimes
        people can get into more trouble with dynamically generated JS if they
        aren't used to doing that, regardless of what language they're using on
        the back end to generate with. I had a tough time when I was starting
        out with that stuff.

        You might start by checking what the browser is actually getting for
        your dynamically and statically generated cases, by using the "view
        source" option

        Nathan Gilbert wrote:[color=blue]
        > Randy Webb <HikksNotAtHome @aol.com> once pondered:[color=green]
        > > Nathan Gilbert said the following on 6/26/2006 5:33 PM:[color=darkred]
        > >> I am wanting to use javascript to select between different *.css files
        > >> dependent on the user's browser.[/color]
        > >
        > > If you want one set of stylesheets for IE and another for all other
        > > browsers, then use IE conditionals and not javascript for it.
        > >[/color]
        >
        > Thanks for the tip, I replaced the javascript with IE conditionals.
        > Everything works now the way I intended.
        >
        > Is there any trick to getting PERL generated javascript to execute in
        > the user's browser? Can someone point out a tutorial that explains how
        > and when most browsers parse and execute javascript?
        >
        > Maybe if I can find out more about how browsers work, I can figure out
        > why my original method didn't work, just for curiosity.
        >
        > Thanks in advance,
        > NG
        > --
        > "The life of a repoman is always intense."[/color]

        Comment

        Working...