Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  191] [ 3]  / answers: 1 / hits: 140907  / 11 Years ago, mon, february 25, 2013, 12:00:00

How to use if statement inside JSON Here is the code:
.......................................................................................



var config =
[
{
name : SiteTitle,
bgcolor : ,
color : ,
position : TL,
text : step1,
time : 5000
},
{
name : Jawal,
bgcolor : ,
color : ,
text : step2,
position : BL,
time : 5000
},
{
name : Password,
bgcolor : ,
color : ,
text : step3,
position : TL,
time : 5000
}
],

//define if steps should change automatically
autoplay = false,
//timeout for the step
showtime,
//current step of the tour
step = 0,
//total number of steps
total_steps = config.length;


This is the required result something like this:



    var config =
[

if(page==true) {
{
name : SiteTitle,
bgcolor : ,
color : ,
position : TL,
text : step1,
time : 5000
},
{
name : Jawal,
bgcolor : ,
color : ,
text : step2,
position : BL,
time : 5000
}
} else {
{
name : Password,
bgcolor : ,
color : ,
text : step3,
position : TL,
time : 5000
}
}
],

//define if steps should change automatically
autoplay = false,
//timeout for the step
showtime,
//current step of the tour
step = 0,
//total number of steps
total_steps = config.length;


Actually this way is wrong and makes a JavaScript syntax error.


More From » jquery

 Answers
41

That's regular JavaScript, not JSON. Move the if statement outside:



if (page) {
var config = [
{
name : SiteTitle,
bgcolor : ,
color : ,
position : TL,
text : step1,
time : 5000
}, {
name : Jawal,
bgcolor : ,
color : ,
text : step2,
position : BL,
time : 5000
}
];
} else {
var config = [
{
name : Password,
bgcolor : ,
color : ,
text : step3,
position : TL,
time : 5000
}
];
}

[#80025] Saturday, February 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yaquelina

Total Points: 517
Total Questions: 101
Total Answers: 96

Location: Egypt
Member since Tue, Jul 6, 2021
3 Years ago
yaquelina questions
;