pashanoid, importing the :html2 paramater after use CGI: will enable the comment and Link methods to be utilised. Using these you can construct your conditional comments and use the start_html method, for example:
- Set up your code, Importing the html2 methods. I'm in html4 so I include the no_xhtml flag too.
use strict;
use warnings;
use CGI qw/:html2 -noxhtml/;
- Before the start_html method set up a sub which reproduces the Link method
sub linkstyle(){
Link { -rel => 'stylesheet',
-type => 'text/css',
-src => $arebelong.'script/ie2.css',
-media => 'all'
}
};
- Now within the start_html call the head method with a reference to an array that includes the comment method which includes the call to the relevant sub for the conditional
print $q->start_html(
-head=>[
comment('[if lte IE 6]>'."\n".&linkstyle.'<![endif]')
]
);
- Put them all together and add a few methods so that the ref to an array is a ref to an array (and not a ref to an array holding one item which could be held by a scalar (this is for a conditional after all)) and you can compare a comment and link to a stylesheet with a forged conditional comment
#! /usr/bin/perl
use strict;
use warnings;
use CGI qw/:html2 -noxhtml/;
use CGI::Pretty;
my $allyour = "http://localhost/";
my $arebelong = "http://localhost/";
my $normalstyle = "http://localhost/";
my $normalscript = "http://localhost/";
my $q = CGI->new();
print $q->header(-type=>'text/html');
sub linkstyle(){
Link({-rel => 'stylesheet',
-type => 'text/css',
-src => $arebelong.'to/us/script/ie2.css',
-media => 'all'
})
};
print $q->start_html(
-lang=>'en-GB',
-title=>'conditionalcomments or hot css buns',
-head=>[
Link({-rel => 'stylesheet',
-type => 'text/css',
-src => $allyour.'buns/script/ie8.css',
-media => 'all'
}),
comment('exists (conditional comment method) ? find : next'),
comment('[if lte IE 6]>'."\n".&linkstyle."\n".'<![endif]'),
],
-style=>{-src=> $normalstyle.'style/w3m.css', -media=>'all'},
-script=>{-type=>'text/javascript',
-src=> $normalscript.'script/nom/nom.js'}
);
print $q->h1('HelloWorld!!'),
$q->end_html;
exit 0;
This returns the various link elements to stylesheets as required.
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en-GB"><head><title>conditionalcomments or hot css buns</t
+itle>
<link type="text/css" media="all" src="http://localhost/buns/script/ie
+8.css" rel="stylesheet">
<!-- exists (conditional comment method) ? find : next -->
<!-- [if lte IE 6]>
<link type="text/css" media="all" src="http://localhost/to/us/script/i
+e2.css" rel="stylesheet">
<![endif] -->
<link rel="stylesheet" type="text/css" href="http://localhost/style/w3
+m.css"media="all">
<script src="http://localhost/script/nomnom.js" type="text/javascript"
+></script>
</head>
<body>
<h1>
HelloWorld!!
</h1>
</body>
</html>
One thing I am not too sure about is whether the 'rel' attribute not being first in order within the link element will make a difference, I have a niggle that it is supposed to be first from somewhere but not sure. However this occurs when the link method is called both straightforwardly within the head method and when called as a return from a sub so probably is ok.
I would question why you are trying to produce conditional comments from a server cgi script though. As I would expect these are mostly used in shtml. Unless of course you are producing static shtml for future usage. Then though, wouldn't there now be a need to ensure the correct DTD is in use? Would be interesting to know.
Coyote
|