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
Pedro PaivaPedro Paiva 

Is there a best practice to redirect to URL in a lightning component ?

I built a lightning component from where i call to a external URL using window.location.href = 'someurl'. But i don't know if that is a good practice in Salesforce Lightning Experience. I also tried with sforce but i would need to add it through connection.js and apex.js and from the lightning component itself i can't. (Sorry for my bad english, i am not a native english speaker)

Thanks in advance :)
Best Answer chosen by Pedro Paiva
Ashif KhanAshif Khan
Hi Pedro,

In Lightning force:navigateToURL   opens new tab for external URLs if you want to open in new tab you can use navigateToURL
 
var eUrl= $A.get("e.force:navigateToURL");
    eUrl.setParams({
      "url": 'https://www.youraddress.com/' 
    });
    eUrl.fire();

in case you want to open in the same tab you need to use standard javascript as 
window.open('www.youraddress.com','_top')
I hope this will help you

Regards
Ashif
 

All Answers

GarryPGarryP
you should use force:navigateToURL action in lightning
navigate : function(component, event, helper) {

    //Find the text value of the component with aura:id set to "address"
    var address = component.find("address").get("v.value");

    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      "url": 'https://www.google.com/maps/place/' + address
    });
    urlEvent.fire();
}
Details can be seen here -
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_force_navigateToURL.htm
 
Ashif KhanAshif Khan
Hi Pedro,

In Lightning force:navigateToURL   opens new tab for external URLs if you want to open in new tab you can use navigateToURL
 
var eUrl= $A.get("e.force:navigateToURL");
    eUrl.setParams({
      "url": 'https://www.youraddress.com/' 
    });
    eUrl.fire();

in case you want to open in the same tab you need to use standard javascript as 
window.open('www.youraddress.com','_top')
I hope this will help you

Regards
Ashif
 
This was selected as the best answer
warren jonnwarren jonn
window.location.replace('http://w3c.com');
Using replace() is better for javascript redirect (http://net-informations.com/js/iq/load.htm) , because it does not keep the originating page in the session history, meaning the user won’t get stuck in a never-ending back-button fiasco.