It would be good to have the conditional comments feature extend to scripts in the .info file.

For example the script for DD-belatedPNG to fix issues with the IE6 transparency and should only be called for IE6. http://www.dillerdesign.com/experiment/DD_belatedPNG/

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Standart’s picture

Exactly the same use case here.

JohnAlbin’s picture

I'd consider this. Especially if someone else wrote the patch. :-D

nairb’s picture

Version: 6.x-1.1 » 6.x-1.x-dev
FileSize
3.4 KB

I modified 6.x-1.x-dev to include conditional script support. I am including a patch. See what you think.

Here is the formatting in my theme.info:
; Set the conditional scripts that are processed by IE.
conditional-scripts[if IE 6][] = js/DD_belatedPNG_0.0.8a-min.js
conditional-scripts[if IE 6][] = js/ie6.js

Brian

jdanthinne’s picture

FileSize
3.56 KB

Patch is working fine for me.
Modified it to be able to include extrernal scripts as well.

gmclelland’s picture

Tested both patches. Both patches applied, but had no effect.

Not sure why, but I couldn't get the js to load.

Here is the link in my .info file
conditional-scripts[if IE 6][] = "js/pngfix-1.1/jquery.pngFix.js"

Used the latest 6.x-1.x-dev version.

I am using the TAO theme as my base theme.

gmclelland’s picture

I think my problem isn't the patches, it's that the conditional styles module simply doesn't work with TAO. Anyone have any suggestions on how to fix it?

I tried adding $var['styles'] .= $var['conditional_styles']; inside function tao_preprocess_page

Thanks

nairb’s picture

Threw TAO on my dev site and got it working...

Use jdanthinne's patch. He also fixed a var that I missed. :)

For your preprocess, use 'vars' instead of 'var':
$vars['styles'] = drupal_get_css($css);
$vars['styles'] .= $vars['conditional_styles'];

Make sure the conditional css/js files exist too, the module checks.

Brian

nairb’s picture

Status: Active » Needs review
gmclelland’s picture

Hi @nairb, thanks for the help. I tried adding

function tao_preprocess_page(&$vars) {
$vars['styles'] = drupal_get_css($css);
$vars['styles'] .= $vars['conditional_styles'];

to my template.php, but then my page renders really funky looking. It's almost as if the styles weren't processed by the Scaffold module. Maybe there is some kind of conflict? Even with the messed up page I checked the view source in IE 6 and it didn't include my conditional script.

I also double checked the paths to my js files as specified in the .info file.

When I remove the code from the template.php everything goes back to working fine.

Any suggestions, I would love to get this working.

gmclelland’s picture

FYI.. I'm using the 1.x branch of Scaffold. It looks like this issue might be what is causing the problem:
http://drupal.org/node/885056

Possible fix, but I couldn't get it to work when I combine your code with what is provided.
http://drupal.org/node/885056#comment-3390290

gmclelland’s picture

@nairb - Does it matter that TAO outputs the scripts at the bottom of the page?
Should it be $vars['scripts'] since it is a script that needs to be outputted with the $scripts variable not the $styles variable?

or maybe it should be $vars['conditional_scripts'] ?

Also, I read that function _scaffold_get_css($css = NULL) is the replacement of drupal_get_css()

So technically I should be able to this:

function tao_preprocess_page(&$vars) {
$vars['styles'] = _scaffold_get_css($css);
$vars['styles'] .= $vars['conditional_styles'];

Seems like the code above would just make TAO + conditional_styles module play nicely together, but not javascripts. Am I wrong?

gmclelland’s picture

Tried this, but it didn't seem to work. FYI.. I am clearing all caches before I check the results

function cmftao_preprocess_page(&$vars) {

$vars['styles'] = _scaffold_get_css($css);
$vars['styles'] .= $vars['conditional_styles'];

$vars['scripts'] = drupal_get_js();
$vars['scripts'] .= $vars['conditional_scripts'];

nairb’s picture

You only need to add the additional line to the preprocess_page function for the css styles because the Tao theme modifies their media attribute, otherwise the module handles everything automatically. That line will appended the conditional styles once Tao is done making changes. Tao does nothing with the js, so there is no reason to add those lines to the preprocess_page.

#11 sounds plausible but without testing I do not have knowledge of how the module works.

I would begin testing by eliminating as many possible variables as you can. Start with Tao and Conditional Comments only, remove anything else that may modify css/js file. If you can get that working, then try adding Scaffolding.

gmclelland’s picture

I disabled the Scaffold module.

I only placed:
$vars['styles'] = drupal_get_css($css);
$vars['styles'] .= $vars['conditional_styles'];

into my preprocess_page

The conditional stylesheets show up in my webpage correctly, but I can't get the conditional scripts to output. All my other non-conidtional js files output correctly.

I used devel to check the conditional scripts variable and it did have the correct conditional javascript file specified with the correct paths, but for some reason it simply won't output into the page.

Any other suggestions? Thanks for your help. I will continue to test.

puya’s picture

This is really useful. Could you please commit this to head?

puya’s picture

Oh and is the patch in the dev version or do I still need to patch the dev?

puya’s picture

oops .. looks like the #4 patch broke my site :


Notice: Undefined index: arguments in /.../includes/theme.inc on line 318

nairb’s picture

Status: Needs review » Needs work

If anyone has been having problems with this patch or the module in general, I have come to a couple conclusions... it will break when you use a module that modifies $scripts, such as jQuery Update. The $scripts and $styles variables should be modified through the api or .info file, not added as a string on the end as this is doing.

Looking at this issue, the conditional scripts could be thrown in $head but I don't really like that idea, as I prefer the conditional css and js to be added below their respective sections.

The option I am currently testing, is to use the patch above, remove the portion that updates the $styles/$scripts, and insert the $conditional_styles/$conditional_scripts variables into my template where I want them. It would be nice to automate this process, so you could just activate the module and be done but I do not see how that could be achieved.

JohnAlbin’s picture

Category: feature » task
Priority: Normal » Major

I've been playing around with some code in the D7 version to get conditional comment-wrapped scripts to work. This suddenly became important to me since the HTML5 shim requires it.

klonos’s picture

Title: Add scripts support for conditional comments » Add scripts support for conditional comments - a.k.a. conditional javascript

Really interesting feature! I have no use for it right now, but I'm sure it will come handy someday ;)

muschpusch’s picture

subscribe!

fonant’s picture

This would be nice!

betovarg’s picture

Subscribing. Any intentions in getting this one into the actual module, without a patch? thnx!

loopy1492’s picture

Version: 6.x-1.x-dev » 7.x-2.x-dev

I'd like this for Drupal 7.

nairb’s picture

Version: 7.x-2.x-dev » 6.x-1.x-dev

That's great but this is a 6.x issue.

klonos’s picture

I don't like to impose here, but it is accustomed for features to go against latest version first and then backported. So, changing it to 7.x is more of a "procedural" thing - it doesn't mean it won't happen for 6.x too. Besides, this issue hasn't seen any action recently and it's more likely that by the time this gets implemented, most of us will already be using D7. Also, if you take a look back at #19, John (the maintainer of this module) seems to be playing around with a 7.x version.

I won't join the change-issue-version game though :P

loopy1492’s picture

Okaaaaaay. I guess I'll just copy this issue.

klonos’s picture

...that's not the way either. I'm sure that the issue you'll file will eventually be closed as duplicate of this one. The process of getting things done is as I described: first in latest branch, then backported. Switching an issue to a version/branch or another won' get anybody any attention. Specifically for this issue, it is pointless since the module maintainer already clearly stated that he's working on it in the 7.x branch.

JohnAlbin’s picture

Version: 6.x-1.x-dev » 7.x-2.x-dev

We can work on both D6 and D7 in the same issue. :-)

The patch for D6 needs work. There is no patch for D7. When you post a patch just mention which version its for.

Thanks!

Bedstvie’s picture

FileSize
755 bytes
xurizaemon’s picture

Status: Needs work » Needs review
FileSize
1.09 KB

Adds support for scripts-conditional[]-style entries in theme .info, as per existing config.

Probably don't need to be able to add JS for print mode (do we?) but there you go.

EDIT: This patch (and the tarball above it) aren't complete, because drupal_add_js() doesn't handle the $options['browsers'] parameter that drupal_add_css() does. So your conditional scripts will be applied to all browsers anyway.

On reflection, conditional scripts aren't such a good idea IMO, and I'd encourage using conditionals in the JS instead of this approach.