Executing Javascript generated by PERL

N

Nathan Gilbert

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
 
R

Randy Webb

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:

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

lanaguage? but the language attribute is not needed.

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?
 
N

Nathan Gilbert

Randy Webb said:
Nathan Gilbert said the following on 6/26/2006 5:33 PM:

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
 
D

David McNerney

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top