function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jaredbakerjaredbaker 

JavaScript won't load when !{Account.BillingStreet} points to a two-line address.

I have the following code for pulling up driving directions from SalesForce. Works amazingly well as long as the address is on one line. When it's a two-line address, none of the script will load. I've tried putting the variable into a try/catch rule, but that doesn't solve the problem. Advice?

 

 

 

<apex:page standardController="Account" showHeader="false" sidebar="false" standardstylesheets="false">
<script>

// SOFTWARE BUG!!! MULTI-LINE ADDRESS AND PAGE LOADS W/O JAVASCRIPT!

var currentLocation;

function getLocation()
  {
    navigator.geolocation.getCurrentPosition(showPosition);
  }

function showPosition(position)
  {

try {

    var address = '{!Account.BillingStreet}' + '{!Account.BillingCity},' + '{!Account.BillingState}' + '{!Account.BillingPostalCode}';
    var destination = address.split(' ').join('+');
}
catch(err)
{

    alert('Street address must be on one line to use this function.');
    window.close();
    
}

  currentLocation = position.coords.latitude + ',' + position.coords.longitude;
  
  if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPod/i)))
    {
        var URL = 'http://maps.apple.com/?saddr=' + currentLocation + '&daddr=' + destination;
        window.open(URL,'_parent');
    }
  else
    {
        var URL = 'https://maps.google.com/maps?saddr=' + currentLocation + '&daddr=' + destination;
        window.open(URL,'_parent');
    }
  }
function init()
  {
    document.write('<div style="position:absolute;top:50%;left:50%;">loading...</div>');
    getLocation();
  }

window.onload = new function() { init(); };


</script>

</apex:page>
Best Answer chosen by Admin (Salesforce Developers) 
jaredbakerjaredbaker

Problem resolved by replacing {!Account.BillingStreet} with {!SUBSTITUTE(JSENCODE(Account.BillingStreet),'\r\n',' ')}

All Answers

jaredbakerjaredbaker

Problem resolved by replacing {!Account.BillingStreet} with {!SUBSTITUTE(JSENCODE(Account.BillingStreet),'\r\n',' ')}

This was selected as the best answer
karimulla testingkarimulla testing
HI Jared baker!!  i have a query , are you available here, or can you provide your email id please