logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

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

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!

This question has been answered.

Robert Harm | 02/04/13 at 12:10pm Edit
Tutorial: How to assign prize money


(8) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    02/04/13
    12:14pm
    Francisco Javier Carazo Gil says:

    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.

  • avatar
    Last edited:
    02/04/13
    12:21pm
    John Cotton says:

    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;

  • avatar
    Last edited:
    02/04/13
    12:17pm
    Francisco Javier Carazo Gil says:

    <?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

    );

  • avatar
    Last edited:
    02/04/13
    3:56pm
    Robert Harm says:

    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!

  • avatar
    Last edited:
    02/04/13
    3:58pm
    John Cotton says:

    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....

  • avatar
    Last edited:
    02/04/13
    4:05pm
    Robert Harm says:

    It is the first post on pro mapsmarker.com

  • avatar
    Last edited:
    02/04/13
    4:20pm
    John Cotton says:

    Should be

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

  • avatar
    Last edited:
    02/04/13
    5:06pm
    Robert Harm says:

    ups - thanks!

This question has expired.



Christianto, Robert Harm, Dbranes, Naveen Chand voted on this question.



Current status of this question: Completed



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.