[wp-hackers] wp_localize_script() issue

Philip Walton philip at philipwalton.com
Mon Jun 20 18:34:50 UTC 2011


Thanks Mihai, good to know.

I guess, for my purposes, it's just as easy to echo a JSON object as it 
is to use wp_localize_script(), so I think I'll just do that.


On 6/20/11 12:36 AM, Mihai Chereji wrote:
> On 06/20/2011 10:25 AM, Philip Walton wrote:
>> Put another way, does anyone see a problem if PHP data is passed as 
>> JS with string keys like:
>>
>> var jsVar = {
>>     "string-key": "Foo Bar"
>> };
>>
>> Instead of:
>>
>> var jsVar = {
>>     nonStringKey: "Foo Bar"
>> };
>>
>> I'm not an expert of the subject, but this seems to work just fine 
>> for me. Does anyone see any reason not to change it to this?
>>
>> This would be a really simple patch on line 63 of 
>> "includes/class.wp-scripts.php"
>>
>>
>> On 6/19/11 6:59 PM, Philip Walton wrote:
>>> When using wp_localize_script(), the third parameter accepts an 
>>> array of data to pass.
>>>
>>> I've just noticed that if the keys of the array contain minuses 
>>> (dashes) in them, this will cause errors in your javascript. Firebug 
>>> is telling me "missing : after property id", which I assume is a 
>>> parse error.
>>>
>>> I know I can solve this by not putting minuses in my array key 
>>> names, but for backwards comparability, it would make my life much 
>>> easier if I could in this situation.
>>>
>>> Has anyone else encountered this? Is there a known workaround other 
>>> than removing the minuses from the array keys or json_encoding the 
>>> entire array?
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
> This may well have more deep implications. Even the autosave mechanism 
> in core uses the dot notation to access the translations  (e.g 
> autosave.autosaveL10N.autosaveInterval ) and the minus sign is not a 
> valid character in variable names in JS.
>
> Plus I imagine there a lot of third party scripts that would be broken 
> for the very same reason.
>
> Mihai
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list