Custom Lightning Component – How to open a url in the same window and also be able to open the url by right clicking and opening in a new tab

lightninglightning-aura-components

My goal is to display articles in a custom LC. I am able to achieve this and article links are clickable. On click of the article link opens the article detail in existing window. Working fine. However, on right click and opening the article link in a new tab, I am getting the blank page with error "about:blank#blocked". How do I facilitate the right click and open in new tab both work.. Thank you.

Code snippet for reference –

Lightning component:

<a> onclick="{!c.gotoArticle}" </a>

Lightning Controller

gotoArticle: function(cmp, event){

        var whichPage       = "/article/";
        var whichArticleId  = event.target.getAttribute('data-id');
        var whichArticle    = event.target.getAttribute('data-value');
        var myURL           = whichPage.concat(whichArticle.toString());
        
        var urlEvent = $A.get("e.force:navigateToURL");
        urlEvent.setParams({
            "url": myURL
        });
        urlEvent.fire();                          
    },

                    

Best Answer

your issue is that the anchor tags is missing the href attribute, which is what is used on right click > open in new tab to identify what url to reference.

You will have to use NavigationMixin.GenerateUrl to populate the href attribute with the appropriate url.