Pay money and get answers for your WordPress questions (more info)

Javascript - how to to set the property of a variable correctly?

  • SOLVED

I am the developer of mapsmarker.com and for my pro version I plan to integrate Google Adsense for maps.
Just got one problem: I use wp_localize_script to pass dynamic values to leaflet.js:

wp_localize_script('leafletmapsmarker', 'mapsmarkerjs', array(
'google_adsense_status' => $google_adsense_status,
'google_adsense_format' => $google_adsense_format,
'google_adsense_position' => $google_adsense_position,
'google_adsense_backgroundColor' => $google_adsense_backgroundColor,
'google_adsense_borderColor' => $google_adsense_borderColor,
'google_adsense_titleColor' => $google_adsense_titleColor,
'google_adsense_textColor' => $google_adsense_textColor,
'google_adsense_urlColor' => $google_adsense_urlColor,
'google_adsense_publisherId' => $google_adsense_publisherId
) );


Within leaflet.js I access these variables and use them for the Adsense script:

_initAdSense: function() {
var adUnitDiv = document.createElement('div');
var user_adsense = new String;
user_adsense.format = 'google.maps.adsense.AdFormat.'+mapsmarkerjs.google_adsense_format;
user_adsense.position = 'google.maps.adsense.ControlPosition.'+mapsmarkerjs.google_adsense_position;
user_adsense.backgroundColor = mapsmarkerjs.google_adsense_backgroundColor;
user_adsense.borderColor = mapsmarkerjs.google_adsense_borderColor;
user_adsense.titleColor = mapsmarkerjs.google_adsense_titleColor;
user_adsense.textColor = mapsmarkerjs.google_adsense_textColor;
user_adsense.urlColor = mapsmarkerjs.google_adsense_urlColor;
user_adsense.publisherID = mapsmarkerjs.google_adsense_publisherId;

var adUnitOptions = {
format: user_adsense.format,
position: user_adsense.position,
backgroundColor: user_adsense.backgroundColor,
borderColor: user_adsense.borderColor,
titleColor: user_adsense.titleColor,
textColor: user_adsense.textColor,
urlColor: user_adsense.urlColor,
map: this._google,
visible: true,
publisherId: user_adsense.publisherID
}
this._adUnit = new google.maps.adsense.AdUnit(adUnitDiv, adUnitOptions);
}


This works fine for all options but format and position where I get the error
"Uncaught Error: Invalid property for value <format>: google.maps.adsense.AdFormat.HALF_BANNER"


I already tried changing the code to

user_adsense.format = mapsmarkerjs.google_adsense_format;
user_adsense.position = mapsmarkerjs.google_adsense_position;
var adUnitOptions = {
[...]
format: google.maps.adsense.AdFormat.user_adsense.format,
position: google.maps.ControlPosition.user_adsense.position,


but unfortunately with no success.
Any ideas why this is not working?
Thanks!

Answers (2)

2013-02-04

John Cotton answers:

Is it because you're trying to set an int value with a string:


user_adsense.format = 'google.maps.adsense.AdFormat.'+mapsmarkerjs.google_adsense_format;

should be

user_adsense.format = google.maps.adsense.AdFormat[mapsmarkerjs.google_adsense_format];

...assuming mapsmarkerjs.google_adsense_format is storing the string. If it's the id then:

user_adsense.format = mapsmarkerjs.google_adsense_format;


Robert Harm comments:

Hi John,

this worked for google_adsense_format - thanks. Unfortunately not for google_adsense_position - when I add the following:


user_adsense.format = google.maps.adsense.AdFormat[mapsmarkerjs.google_adsense_format];
user_adsense.position = google.maps.adsense.ControlPosition[mapsmarkerjs.google_adsense_position];

format: user_adsense.format,
position: user_adsense.position,


I get the following error:
TypeError: google.maps.adsense.ControlPosition is undefined - line 164 which is

user_adsense.position = google.maps.adsense.ControlPosition[mapsmarkerjs.google_adsense_position];

what am I missing here?
thanks for your help!


John Cotton comments:

Can you send me a link to a page with this on? I need to see what values you are actually holding in mapsmarkerjs...it's going to be a string/int thing....


Robert Harm comments:

It is the first post on pro mapsmarker.com


John Cotton comments:

Should be
user_adsense.position = google.maps.ControlPosition[mapsmarkerjs.google_adsense_position];


Robert Harm comments:

ups - thanks!

2013-02-04

Francisco Javier Carazo Gil answers:

Try to set the array out and the pass the name, the definition tell to pass the array name, not the array.

Referencing issues I think.


Francisco Javier Carazo Gil comments:

<?php wp_localize_script( $handle, $object_name, $l10n ); ?>

No, I confuse, but try in anyway to create the array before.


$myArray = array(
'google_adsense_status' => $google_adsense_status,
'google_adsense_format' => $google_adsense_format,
'google_adsense_position' => $google_adsense_position,
'google_adsense_backgroundColor' => $google_adsense_backgroundColor,
'google_adsense_borderColor' => $google_adsense_borderColor,
'google_adsense_titleColor' => $google_adsense_titleColor,
'google_adsense_textColor' => $google_adsense_textColor,
'google_adsense_urlColor' => $google_adsense_urlColor,
'google_adsense_publisherId' => $google_adsense_publisherId

);