Friday, April 26, 2013

Re: Calling Jquery or javascript function based on an if condition

A slight variation of this approach is to map some Django template context info to Javascript variables in one of your templates, making that data accessible to your compressed / minified JS code:

# in a template, "base.html" perhaps..

<script>
window.DjangoContext = {
  userLoggedIn: {{ user.is_authenticated|yesno:"true,false" }}
}
</script>

Note the use of the 'yesno' filter to convert "True" to "true" to match JS syntax.

Now you can access DjangoContext.userLoggedIn anywhere in your JS.


** It's important to note that users can modify any JS variable they wish, so don't rely on something like this for security **

On Tuesday, 23 April 2013 16:45:20 UTC-4, Сергей Костюченко wrote:

{% if condition %}

<script type="text/javascript">

some_function(params);

</script>

{% endif %}

Sarfraz ahmad wrote at 23 апреля 2013 13:30:38:

Guys, is it possible to call a javascript function in django templates based on an if condition.......?????????? What i want to do is that in django templates if a given if condition is true i want to call a javascript function automatically............

tell me guys if it is possible or not????



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment