+ Start a Discussion
admintrmpadmintrmp 

[Bug?] Visualforce/JavaScript problems

I've noticed that when I do for-loops in Javascript in Visualforce (I know, I know! I should put my JavaScript in a JS file!) I'ved noticed that it sometimes gets messed up, particuarly when there are two or more for-loops in the script!

 

This not the first time I've experienced this and it can be achieved quite simply.

 

 

<script type="text/javascript">
function selectLoose(objForm)
{
var records = '';
var els = objForm.elements;

for ( var i=0; i<els.length; i++ )
{
if ( els[i].nodeName.toLowerCase() == 'input' && els[i].getAttribute('type').toLowerCase() == 'checkbox' )
{
if ( els[i].value.indexOf('loose_') == 0 && els[i].checked )
{
records += (records == '' ? '' : ';') + els[i].value.substring(els[i].value.indexOf('_') + 1);
}
}
}

if ( records == '' )
{
alert('You have not selected any loose records to be deleted.');
return false;
}

getObj('delId').value = records;
return false;
}

function selectAllLoose(objAll)
{
var records = '';
var els = objAll.form.elements;

for ( var i=0; i<els.length; i++ )
{
if ( els[i].nodeName.toLowerCase() == 'input' && els[i].getAttribute('type').toLowerCase() == 'checkbox' )
{
if ( els[i].value.indexOf('loose_') == 0 )
{
els[i].checked = objAll.checked;
}
}
}
}
</script>

 

 The code above gets parsed into:

 

 

<script type="text/javascript">
function selectLoose(objForm)
{
var records = '';
var els = objForm.elements;

for ( var i=0; i<els records="" .length; i++ )
{
if ( els[i].nodeName.toLowerCase() == 'input' && els[i].getAttribute('type').toLowerCase() == 'checkbox' )
{
if ( els[i].value.indexOf('loose_') == 0 && els[i].checked )
{
records += (records == '' ? '' : ';') + els[i].value.substring(els[i].value.indexOf('_') + 1);
}
}
}

if ( records == '' )
{
alert('You have not selected any loose records to be deleted.');
return false;
}

getObj('delId').value = records;
return false;
}

function selectAllLoose(objAll)
{
var ;
var els = objAll.form.elements;

for ( var i=0; i<els.length; i++ )
{
if ( els[i].nodeName.toLowerCase() == 'input' && els[i].getAttribute('type').toLowerCase() == 'checkbox' )
{
if ( els[i].value.indexOf('loose_') == 0 )
{
els[i].checked = objAll.checked;
}
}
}
}
</script>

 

 

Message Edited by admintrmp on 10-22-2009 09:19 AM
Best Answer chosen by Admin (Salesforce Developers) 
ThomasTTThomasTT

Put " " (blank space) before and after all "<" and ">".

VF page parser thinks that it is a beginning or ending of HTML tag...

And if possible, try to avoid using single-quote. In Javascript, you can use double-quote.

Even in the double-quote, you can use double-quote by using \" in HTML attribute.

That's one thing we should be careful when we blindly convert a S-Control to a VF page...

ThomasTT

Message Edited by ThomasTT on 10-22-2009 12:42 PM

All Answers

ThomasTTThomasTT

Put " " (blank space) before and after all "<" and ">".

VF page parser thinks that it is a beginning or ending of HTML tag...

And if possible, try to avoid using single-quote. In Javascript, you can use double-quote.

Even in the double-quote, you can use double-quote by using \" in HTML attribute.

That's one thing we should be careful when we blindly convert a S-Control to a VF page...

ThomasTT

Message Edited by ThomasTT on 10-22-2009 12:42 PM
This was selected as the best answer
admintrmpadmintrmp

Ah! Thankyou for that insight.

 

Luckily I never used S-Controls in the first place so I do not have to suffer with that confusion of having to convert everything. :)

 

 

ThomasTTThomasTT

Yeah, it's just for the search. Somebody else may find this post for other reasons.

 

Oh, by the way, I agree. It's not "[Bug?]", it's "[Bug]". However, it's already very known. Even in merge field, if you use > like {!IF(value>3, 'too many', 'too short')} may not work and work around is {!IF(value<=3, 'too short', 'too many')}... (I found such a post somewhere...) also, this means SFDC doesn't parse VF page as HTML/javascript, so it will be very difficult for them to fix this even though it is a bug.

ThomasTT

Message Edited by ThomasTT on 10-23-2009 10:26 AM