Conditionally loading javascript in views

#1
[eluser]codeboy[/eluser]
Hey All,
When I load a view, I am trying to conditionally create JavaScript statements using the syntax below. Since all of this is still happening in the view (and on the server), I expected the conditional statements to be applied (in the javascript section) but it does not seem to be. The "php if" inside the javascript section does not seem to work.

Has anyone tried something similar or do you know if there is something wrong with this?

Any help is much appreciated.

-c

Code:
//This works. Only if load_country is true, does this div appear in the html, which is what I want
<?php if ($load_country): ?>      
   <div id="dialog_country_search">
       &lt;?= form_input('country', $country); ?&gt;
   </div>
&lt;?php endif ?&gt;

&lt;script type="text/javascript"&gt;  

    //This conditional statement does not seem to work and the javascript shows up in the html all the time causing errors
    //since these datatables dont exist
               &lt;?php if ($load_state): ?&gt;      
                  $('#datatable_state').dataTable().fnClearTable();
               &lt;?php endif ?&gt;
                
                &lt;?php if ($load_county): ?&gt;      
                  $('#datatable_county').dataTable().fnClearTable();
                &lt;?php endif ?&gt;
            };          
        &lt;?php endif ?&gt;
&lt;/script&gt;

#2
[eluser]smilie[/eluser]
Hi,

Well it should work - I have also several views with mix of PHP / JS (jQuery).

Only error I see in your code is that you are closing 1 PHP end if too many...

If you have FF installed, install Firebug it may provide you with more info.

Cheers,
Smilie

#3
[eluser]codeboy[/eluser]
Thanks, appreciate the response! The additional php tags in the javascript section are because of bad copy/paste but in my code they match out.

Well, I have firebug, and in there I see that the code that should have been excluded is being included. It appears that if the php tags exist inside the javascript section they are being ignored.

#4
[eluser]InsiteFX[/eluser]
You can try this!
Remove + from script tags.

Code:
// This conditional statement does not seem to work and the javascript
// shows up in the html all the time causing errors

// since these datatables dont exist
&lt;?php if ($load_state): ?&gt;
    <+script type="text/javascript">
        $('#datatable_state').dataTable().fnClearTable();
    <+/script>
&lt;?php endif ?&gt;
                
&lt;?php if ($load_county): ?&gt;
    <+script type="text/javascript">
        $('#datatable_county').dataTable().fnClearTable();
    <+/script>
&lt;?php endif ?&gt;

InsiteFX

#5
[eluser]pickupman[/eluser]
It doesn't look like you have used the jQuery ready function to make the code run on page load.

#6
[eluser]codeboy[/eluser]
Thanks to all who took the time to reply.

This is working. It was a stupid programming error on my part. I was not loading the booleans properly.

Thanks again everyone!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.