You need to sign in to do that
Don't have an account?

Please Help!!!..Javascript for loop
Hi, I have a Javascript function onload which has a for loop..But the for loop is not working.Its throwing some absurd error. Please help
Below is the code & the error:
Code:
window.onload = function()
{
if(("{!$CurrentPage.parameters.id}").length > 0)
{
alert('Im in begin function');
var Sprod = ("{!PSS.AT_Product__c}").split(';');
alert(Sprod.length);
var mainElem=document.getElementById(Hidden);
var sElem=document.getElementById("Productd_selected");
var uElem=document.getElementById("Productd_unselected");
var sI=0;
var uI=0;
sElem.length=0;
uElem.length=0;
alert(mainElem.length);
for (i=0;i<Sprod.length;i++)
{
mainElem.options[parseInt(Sprod[i])].selected=true;
}
}
}
The error which I 'm getting is:
Uncaught SyntaxError:Unexpected identifier
On viewing the page source what I get is:
window.onload = function() | |
{ | |
if(("*******************").length> 0) | |
{ | |
alert('Im in begin function'); | |
var Sprod = ("abc;cde;def").split(';'); | |
alert(Sprod.length); | |
var mainElem=document.getElementById(Hidden); | |
var sElem=document.getElementById("Productd_selected"); | |
var uElem=document.getElementById("Productd_unselected"); | |
var sI=0; | |
var uI=0; | |
sElem.length=0; | |
uElem.length=0; | |
alert(mainElem.length); | |
insert(); | |
for (i=0;i<Sprod label="Chosen" .length;i++) | |
{ | |
mainElem.options[parseInt(Sprod[i])].selected=true; | |
} | |
} | |
} |
HI
After much of research what I found out was that if we have for loop in javascript in VF pages then the condition should have spaces between it i.e.
Its because if we dont have a space in condition i.e. i<TAbleRows then the interpreter assumes it as a VF tag.
This is a bug in VF..
so always add spaces between conditions.
All Answers
Can you repost your code using the Insert Code link (clipboard icon with a little C in it), as some of your code is turning into smileys.
IGNORE THIS POST!!
The Code:
window.onload = function(){
if(("{!$CurrentPage.parameters.id}".length > 0){
if(("{!$CurrentPage.parameters.id}".length > 0){
alert('Im in begin function');
var Sprod = ("{!PSS.AT_Product__c}".split(';');
alert(Sprod.length);
var mainElem=document.getElementById(Hidden);
var sElem=document.getElementById("Productd_selected");
var uElem=document.getElementById("Productd_unselected");
var sI=0;
var uI=0;
sElem.length=0;
uElem.length=0;
alert(mainElem.length);
for (i=0;i<Sprod.length;i++){
mainElem.options[parseInt(Sprod[i])].selected=true;
}
}
}
and the error:
window.onload = function(){
if(("oo6abcdefghi123".length> 0){
alert('Im in begin function');
var Sprod = ("abc;cde;def").split(';');
alert(Sprod.length);
var mainElem=document.getElementById(Hidden);
var sElem=document.getElementById("Productd_selected");
var uElem=document.getElementById("Productd_unselected");
var sI=0;
var uI=0;
sElem.length=0;
uElem.length=0;
alert(mainElem.length);
for (i=0;i<Sprod label="Chosen" .length;i++){
mainElem.options[parseInt(Sprod[i])].selected=true;
}
}
}
The brackets don't match on the first two if statements (although looking at your code maybe its only one?) - both open two round brackets but only close one.
I realise this isn't the issue that you have highlighted, but often with javascript you have to move in baby steps.
I closed the bracket but the error is still not resolved.. Do you any idea why am I getting the label="chosen" in for loop condition even when I havent mentioned it ?
Its the first time I'm facing such issue..
I tried using a do..while statement & to my surprise it worked but if I implement the same using for loop I get an error with page source showing that label thing in the code
No idea at all why that would be.
That said, I haven't seen the entire page and controller, so its possible there's something else causing that to happen.
Sorry to bring up an old post, but is there any fix for this?
I have a javascript for loop and salesforce is randomly moving around my code causing it to error.
Salesforce keeps on rendering this code:
as this:
Which would appear to be the same issue as the original poster
HI
After much of research what I found out was that if we have for loop in javascript in VF pages then the condition should have spaces between it i.e.
Its because if we dont have a space in condition i.e. i<TAbleRows then the interpreter assumes it as a VF tag.
This is a bug in VF..
so always add spaces between conditions.