0

This question already has an answer here:

I have created onclick javascript button functionality but when I clicked on button I am getting "Problem with onclick javascript for this button or link was encountered: Invalid or Unexpected token". My code is

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")} 

function post_to_url(path, method)
{ 
method = method || 'post'; // Set method to post by default, if not specified. 
var form = document.createElement('form'); 
form.setAttribute('method', method); 
form.setAttribute('action', path); 
form.setAttribute('target', '_blank'); 
document.body.appendChild(form); 
form.submit(); 
}

var address1 =     
'{!URLENCODE(Account.BillingStreet)}';

address1.replace(/\r?\n|\r/g, '');

var s1 = http://www.another_page.com &param=

{!URLENCODE(Account.IOG_Customer_ID__c)}
%0D

{!URLENCODE(Account.Website)}
%0D

{!URLENCODE(Account.Name)}
%0D

{!URLENCODE(Account.Billing_Contact_Street__c)}
%0D

{!URLENCODE(Account.Billing_Contact_City__c)}
%0D

{!URLENCODE(Account.Billing_Contact_State__c)}
%0D

{!URLENCODE(Account.Billing_Contact_Zip__c)}
%0D

{!URLENCODE(Account.Phone)}
'
;

post_to_url(s1, 'POST');

marked as duplicate by glls, battery.cord, Alex Tennant, Martin Lezer, Brian Mansfield Mar 19 '18 at 13:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

the Javascript is not valid as it's missing the quote ' at the start and you either need to have the string literal in one line or escape each line with \ at the end of the line.

You can see the fixed example of your code here:

var s1 = 'http://www.another_page.com&param={!URLENCODE(Account.IOG_Customer_ID__c)}%0D{!URLENCODE(Account.Website)}%0D{!URLENCODE(Account.Name)}%0D{!URLENCODE(Account.Billing_Contact_Street__c)}%0D{!URLENCODE(Account.Billing_Contact_City__c)%0D{!URLENCODE(Account.Billing_Contact_State__c)}%0D{!URLENCODE(Account.Billing_Contact_Zip__c)}%0D{!URLENCODE(Account.Phone)}';
  • 2
    Please elaborate on the op’s problem/error rather than posting a corrected code dump with no explanation whatsoever so it may prove useful to others facing similar problems – glls Mar 17 '18 at 15:46
  • @glls I've stated "I don't think it's valid Javascript hence the error." How should I improve on this? – vladykx Mar 17 '18 at 16:19
  • for example, how is it not valid javascript syntax? – glls Mar 17 '18 at 16:24