ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Segments and conditional jQuery problem

August 10, 2010 8:37am

Subscribe [2]
  • #1 / Aug 10, 2010 8:37am

    SarahBenfield

    2 posts

    Hi All,

    I’m trying to do some basic segment checking and write out jQuery code dependant on those segments. The first if statement works and outputs the correct jQuery as expected, however when I do another segment check it actually writes out the values of those segments. Here’s my code:

    [removed]
      $(document).ready(function() {
    
        {if segment_1 == "locations"}
        // locations form jQuery
        $('#ctl00_cphContent_address_qs').hide();
        $('#txtFilter').hide();
        $('#filter').click(function()
        {
            $('#checkBoxes').slideToggle();
        });
        {/if}
    
    {if segment_1 == "locations" && segment_2 == "results"}
        $('#ctl00_cphContent_address_qs').hide();
                $('#ctl00_cphContent_submitbtn').hide();
                $('#ctl00_cphContent_txtFilter').hide();
                $('.business-filters').hide();
                $('#cbxChangeFilters').click(function()
                {
                    if ($('#cbxChangeFilters').is(':checked'))
                    {
                        $('.business-filters').show('slow');
                    }
                    else
                    {
                        $('.business-filters').hide('slow');
                    }
                });
                var zIndexNumber = 1000;
                $('div').each(function() {
                    $(this).css('zIndex', zIndexNumber);
                    zIndexNumber += 10;
                });
                if($.browser.name == 'msie' && $.browser.versionX == 6)
                {
                    $('#oldie6').html("You are using a browser that we no longer support. This page may therefore not look or function as we intended.
    For a better web experience, we recommend that you upgrade your browser to the latest version.<ul class='browserList'><li><a href="http://www.mozilla-europe.org/en/firefox/">Mozilla Firefox</a></li><li><a href="http://www.google.com/chrome">Google Chrome</a></li><li><a href="http://www.microsoft.com/windows/internet-explorer/default.aspx">Internet Explorer</a></li><li><a href="http://www.apple.com/safari/download/">Safari</a></li></ul>");
                    $('#oldie6').show();
                }
    {/if}
    
      });
    [removed]

    My line that states
    {if segment_1 == "locations" AND segment_2 == "results"} outputs
    {if "locations" == "locations" AND "results" == "results"}, as if the segments have already been parsed but the first segment check runs correctly and writes out the relevant code.

    I’m using v1.6.9 build 20100430.

    Any ideas why this is happening and any remedies?

    Thanks,
    Brett

  • #2 / Aug 10, 2010 11:07am

    Aaron Fowler

    113 posts

    Hi Brett (Sarah?)

    I think this is a function of how EE parses advanced conditionals.  By default it won’t parse advanced conditionals inside of javascript.  So, the segment_n variables are replaced, but the conditionals never run.  You could try using simple conditionals to see if that works.

    From the docs:
    protect_javascript
    Value: y/n
    Prevents the advanced conditionals parser from processing anything in tags. By default, it’s set to ‘y’.
    $config[‘protect_javascript’] = ‘y’;


    cheers,
    Aaron

  • #3 / Aug 10, 2010 11:12am

    Aaron Fowler

    113 posts

    I would wrap the whole of the JS code in the first conditional, then wrap the inner part with the second conditional:

    [removed]
    {if segment_1 == "locations"}
      $(document).ready(function() {
    
        // locations form jQuery
        $('#ctl00_cphContent_address_qs').hide();
        $('#txtFilter').hide();
        $('#filter').click(function()
        {
            $('#checkBoxes').slideToggle();
        });
    
      {if segment_2 == "results"}
        $('#ctl00_cphContent_address_qs').hide();
                $('#ctl00_cphContent_submitbtn').hide();
                $('#ctl00_cphContent_txtFilter').hide();
                $('.business-filters').hide();
                $('#cbxChangeFilters').click(function()
                {
                    if ($('#cbxChangeFilters').is(':checked'))
                    {
                        $('.business-filters').show('slow');
                    }
                    else
                    {
                        $('.business-filters').hide('slow');
                    }
                });
                var zIndexNumber = 1000;
                $('div').each(function() {
                    $(this).css('zIndex', zIndexNumber);
                    zIndexNumber += 10;
                });
                if($.browser.name == 'msie' && $.browser.versionX == 6)
                {
                    $('#oldie6').html("You are using a browser that we no longer support. This page may therefore not look or function as we intended.
    For a better web experience, we recommend that you upgrade your browser to the latest version.<ul class='browserList'><li><a href="http://www.mozilla-europe.org/en/firefox/">Mozilla Firefox</a></li><li><a href="http://www.google.com/chrome">Google Chrome</a></li><li><a href="http://www.microsoft.com/windows/internet-explorer/default.aspx">Internet Explorer</a></li><li><a href="http://www.apple.com/safari/download/">Safari</a></li></ul>");
                    $('#oldie6').show();
                }
      {/if}
    
      });
    {/if}
    [removed]
  • #4 / Aug 10, 2010 11:26am

    bmdstinky

    4 posts

    Aaron,

    Thanks for the reply. That works perfectly. Another one for the ‘lessons learned’ column!

    Thanks again,
    Brett (Sarah!)

  • #5 / Aug 10, 2010 11:34am

    Aaron Fowler

    113 posts

    Yeah, advanced conditionals are a pain.  When possible, I try to separate them out into multiple simple conditionals.  Coming from a programming background, I hate this, but it makes developing/troubleshooting EE much easier.

    -Aaron

  • #6 / Aug 10, 2010 4:44pm

    Ingmar

    29245 posts

    Glad to see Aaron was able to help. Please post again as needed.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases