Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / June 2006



Tip: Looking for answers? Try searching our database.

Executing Javascript generated by PERL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nathan Gilbert - 26 Jun 2006 22:33 GMT
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="javascript"></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.userAgent.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
Signature

"The life of a repoman is always intense."

Randy Webb - 27 Jun 2006 01:12 GMT
Nathan Gilbert said the following on 6/26/2006 5:33 PM:
> I am wanting to use javascript to select between different *.css files
> dependent on the user's browser.

You can't as you have no way of reliably determining the browser.

> I am also wanting to generate the  html document containing this
> javascript dynamically using PERL.

OK, then do.

> 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.

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.

> 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.  

If you want one set of stylesheets for IE and another for all other
browsers, then use IE conditionals and not javascript for it.

> Some of my code samples:
>
[quoted text clipped - 9 lines]
>             <script type="text/javascript" src="./cssSelect.js"
> lanaguage="javascript"></script>

lanaguage? but the language attribute is not needed.

<snip>

> var css = (navigator.userAgent.indexOf("MSIE ") != -1 ? "./css1.css" :
> ../css2.css";

All three browsers that I use daily will pass the 'MSIE' test but only
one is IE.

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

Then why generate static content with PERL?

Signature

Randy
comp.lang.javascript 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/

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

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
Signature

"The life of a repoman is always intense."

David McNerney - 29 Jun 2006 23:46 GMT
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

> Randy Webb <HikksNotAtHome@aol.com> once pondered:
> > Nathan Gilbert said the following on 6/26/2006 5:33 PM:
[quoted text clipped - 16 lines]
> Thanks in advance,
> NG
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.