Search code examples
c#cefsharpchromium-embedded

How to open a link tags in tab control using cefsharp?


I just developed an application like a web browser with the use of Cefsharp library in windows form application. My browser has got several tabs, but whenever user clicks on 'a link tags' with the property of _blank the application opens a new link in a separated window although I want to open links in a new tab in tab control instead.

what I used for my application:

  • C# windows form application

  • ChromiumWebBrowser class from Cefsharp library

I would be pleased if anyone can help.


Solution

  • I've just looked through the documentation and found out the issue.

    • you need to use the LifeSpanHandler class to interrupt the popup and disable opening new browsers by assigning newBrowser to 'null' then you'll be able to perform your instructions.
    • If you want your windows open in self tab, just remove the comment in below code.

    Here's what I've done for my problem:

     public class LifespanHandler : ILifeSpanHandler
        {
    
            bool ILifeSpanHandler.OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
            {
    
                Program.Form.Invoke(new Action(() => Program.Form.newPage(targetUrl)));
    
                //browser.MainFrame.LoadUrl(targetUrl);
    
                newBrowser = null;
                return true;
            }
        }
    

    Hope this is useful for you. :)