[wp-hackers] wp_localize_script() issue

Mihai Chereji mihai.chereji at gmail.com
Mon Jun 20 07:36:58 UTC 2011


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


More information about the wp-hackers mailing list