admin管理员组文章数量:1316030
So I noticed that a piece of Javascript on my VisualForce page was working in some cases, and wasn't in others. JS was doing some operations on a textArea field from one of the custom objects we have. I realized that JS was breaking whenever that textArea field had a new line or carriage return character in it (\n and \r).
So I ended up using a replaceAll() method in the page controller, and removing all of those characters from that textArea field on page load. By the time it got to JS, it was a legal string.
on the VF PAGE:
<script language="JavaScript">
function someFunction() {
var leftOver = 220;
if('{!shippingAddress.Delivery_Requirements__c}'.length > 0){
leftOver -= '{!shippingAddress.Delivery_Requirements__c}'.length;
}
}
</script>
in the controller:
//a fix for the text area field - '\n' and '\r' breaks JS on the VF page
shippingAddress.Delivery_Requirements__c = shippingAddress.Delivery_Requirements__c.replaceAll('\r\n', ' ');
Posting this as a heads up for anyone who encounters JS working for some records and not for others.
If you have insight on why it breaks, do tell.
So I noticed that a piece of Javascript on my VisualForce page was working in some cases, and wasn't in others. JS was doing some operations on a textArea field from one of the custom objects we have. I realized that JS was breaking whenever that textArea field had a new line or carriage return character in it (\n and \r).
So I ended up using a replaceAll() method in the page controller, and removing all of those characters from that textArea field on page load. By the time it got to JS, it was a legal string.
on the VF PAGE:
<script language="JavaScript">
function someFunction() {
var leftOver = 220;
if('{!shippingAddress.Delivery_Requirements__c}'.length > 0){
leftOver -= '{!shippingAddress.Delivery_Requirements__c}'.length;
}
}
</script>
in the controller:
//a fix for the text area field - '\n' and '\r' breaks JS on the VF page
shippingAddress.Delivery_Requirements__c = shippingAddress.Delivery_Requirements__c.replaceAll('\r\n', ' ');
Posting this as a heads up for anyone who encounters JS working for some records and not for others.
If you have insight on why it breaks, do tell.
Share Improve this question asked Feb 28, 2012 at 16:47 Kirill YunussovKirill Yunussov 2,3131 gold badge23 silver badges26 bronze badges2 Answers
Reset to default 6Visualforce has a function called JSENCODE for encoding text and merge field values for use in JavaScript. This function should work for line breaks too.
Visualforce page javascript:
var jsSafeText = "{!JSENCODE(mergeField)}";
It's breaking because JavaScript doesn't allow literal line breaks in strings. This would probably fix it while allowing the line breaks:
public String getDeliveryRequirements() {
return shippingAddress.Delivery_Requirements__c.replace('\r\n', '\\r\\n');
}
Then in the VF page, bind to the getter from above:
if('{!DeliveryRequirements}'.length > 0){
leftOver -= '{!DeliveryRequirements}'.length;
}
Update:
manubkk's answer is better. But I think the correct javascript syntax would be:
var deliveryRequirements = "{!JSENCODE(shippingAddress.Delivery_Requirements__c)}";
本文标签:
版权声明:本文标题:salesforce - Javascript not working on a Visual Force page when new line character is encountered in a String - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741979129a2408292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论