Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140220

Text var to json array

$
0
0

I'm trying to configure header bidding with prebid. I'm loading the ads that I want from database with php + mysql and I create a text variables in javascript with the data from php.

The problem comes when I have to create the variable "adUnits". If I create it manually writting it directly works fine but creating the same content with javascript I don't get it works.

This is the code that works

var adUnits = [{
     code: '/123/468x60',
     mediaTypes: {
         banner: {
             sizes: [
                 [468, 60]
             ]
         }
     },
     bids: [{
         bidder: 'adserver',
         params: {
             networkId: 1234
         }
     }]
 }, {
     code: '/123/336x280',
     mediaTypes: {
         banner: {
             sizes: [
                 [336, 280],
                 [300, 250]
             ]
         }
     },
     bids: [{
         bidder: 'adserver',
         params: {
             networkId: 1234
         }
     }]
 }, {
     code: '/123/300x600',
     mediaTypes: {
         banner: {
             sizes: [
                 [300, 600],
                 [300, 250]
             ]
         }
     },
     bids: [{
         bidder: 'adserver',
         params: {
             networkId: 1234
         }
     }]
 }];

This is the code that doesn't work

var arrayadsname = adsname.split('#');//content of arrayadsname => /123/468x60#/123/336x280#/123/300x600
var arrayadssize = adssize.split('#');//content of arrayadssize => 468x60#336,280|300,250#300,600|300,250
var arraysize = '';
var ad = '';
var adUnits = [];
var preadUnits = '';

for (var i = 0; i < arrayadsname.length; i++) {  
        var arraysizeparts = arrayadssize[i].split('|');

    for (var j = 0; j < arraysizeparts.length; j++) {
                if (j == 0){
                        arraysize = '[' + arraysizeparts[j] + ']';
                }else{
                        arraysize = arraysize + ',' + '[' + arraysizeparts[j] + ']';
                }
            }

        ad = '{code: \'' + arrayadsname[i] + '\', mediaTypes: {banner: {sizes: ' + '[' + arraysize + ']' + '}}, bids: [{bidder: \'adserver\', params: {networkId: 1234}}]}';

        if (i == 0) preadUnits = ad;
        else preadUnits = preadUnits + ',' + ad;
}
//Result of ad => {code: '/123/468x60', mediaTypes: {banner: {sizes: [[468,60]]}}, bids: [{bidder: 'adserver', params: {networkId: 1234}}]},{code: '/123/336x280', mediaTypes: {banner: {sizes: [[336,280],[300,250]]}}, bids: [{bidder: 'adserver', params: {networkId: 1234}}]},{code: '/123/300x600', mediaTypes: {banner: {sizes: [[300,600],[300,250]]}}, bids: [{bidder: 'adserver', params: {networkId: 1234}}]}


adUnits = "[" + JSON.stringify(preadUnits) + "]";

I've tried all posible combinations declare variable asUnits like object, array, text and using JSON.stringify, JSON.parse also declaring "arraysize" like array and parse it. I don't know which is the problem.

I see this error in console: "callee: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Arguments.invokeGetter"

Thanks in advance


Viewing all articles
Browse latest Browse all 140220


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>