最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

salesforce - Javascript not working on a Visual Force page when new line character is encountered in a String - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

Visualforce 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)}";

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论