Yes, this is the best approach because it automatically handles quoting
strings (but leaving numbers unquoted), so you don't have to worry about
the data type.
Anthony
On Thursday, January 29, 2015 at 9:47:05 AM UTC-5, Massimo Di Pierro wrote:
>
> Actually
>
> {{=ASSIGNJS(v = variable)}}
>
> or, equivalent,
>
> var v = {{=XML(json.dumps(variable))}};
>
> is the right thing to do. Not in the manual but is in 2.9.11 and beyond.
>
> On Wednesday, 28 January 2015 17:04:23 UTC-6, José Eloy wrote:
>>
>> Hello!
>>
>> How can I pass a python string to javascript? I know how to pass an
>> integer variable to javascript.
>>
>> Example:
>>
>> def pass_variable():
>> variable = 25
>> return dict(variable=variable)
>>
>> In the view:
>> <script type="text/javascript">
>> var v={{=variable}};
>> console.log(v);
>> </script>
>>
>> If I pass a integer variable, javascript receive it well, but if I pass a
>> string, javascript doesn't recognize it.
>>
>> Now, let's assume that we use variable = "HOLA"
>>
>> I get the error: (using Firefox console)
>> ---------------------------------------------------
>> ReferenceError: HOLA is not defined
>> var v = HOLA;
>> ---------------------------------------------------
>> What am I doing wrong?
>>
>> Regards.
>>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.