By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,569 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,569 IT Pros & Developers. It's quick & easy.

Executing Javascript generated by PERL

P: n/a
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
--
"The life of a repoman is always intense."
Jun 26 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

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

--
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/
Jun 27 '06 #2

P: n/a
Randy Webb <Hi************@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
--
"The life of a repoman is always intense."
Jun 29 '06 #3

P: n/a
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:
Randy Webb <Hi************@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
--
"The life of a repoman is always intense."


Jun 29 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.