Clicking URLs opens default browser
If you're using a WebView
you'll have to intercept the clicks yourself if you don't want the default Android behaviour.
You can monitor events in a WebView
using a WebViewClient
. The method you want is shouldOverrideUrlLoading()
. This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient
of your WebView
using the setWebViewClient()
method.
If you look at the WebView
sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
On image click open URL in new browser not in the old webview
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
context.startActivity(intent)
return true
}
How do I make it so that only *some* links open in the system browser instead of NW.js browser windows?
You don't need to do anything special, it works that way by default. Everything opens in NW.js directly unless you specifically tell it to open in the default browser.
There are many ways to do this, but a simple example would be
<div>
<a href="about.html">About</a>
<a href="thing.html">Thing</a>
<a href="https://example.com" data-external-link>Example</a>
<a href="https://stackoverflow.com" data-external-link>Stackoverflow</a>
</div>
Vanilla JS
// Find all elements on the page
var externalLinks = document.querySelectorAll('[data-external-link]');
// Convert the node list to an array
externalLinks = Array.from(externalLinks);
// loop over each link
externalLinks.forEach(function (link) {
// add a click event listener to each link
link.addEventListener('click', function (evt) {
// Do not navigate to the link in NW.js
evt.preventDefault();
// get the URl for the clicked link
var url = evt.currentTarget.href;
// Open the url in the default browser
nw.Shell.openExternal(url);
});
});
or the jQuery version
$('[data-external-link]').click(function (evt) {
evt.preventDefault();
nw.Shell.openExternal(evt.currentTarget.href);
});
catch link clicks in QtWebView and open in default browser
That does it:
import sys, webbrowser
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://www.az2000.de/projects/javascript-project/"))
web.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
def linkClicked(url): webbrowser.open(str(url.toString()))
web.connect(web, SIGNAL("linkClicked (const QUrl&)"), linkClicked)
web.show()
sys.exit(app.exec_())
WebBrowser-Control - open default browser on click on link
You can open the new page in default browser using Proces.Start() on Navigating event and set e.Cancel = true;
so that the page in the control will not change.
Example:
@MainWindow.xaml.cs
using System.Diagnostics;
using System.Windows;
using System.Windows.Navigation;
namespace OpenDefaultBrowser
{
public partial class MainWindow : Window
{
private static bool willNavigate;
public MainWindow()
{
InitializeComponent();
}
private void webBrowser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
// first page needs to be loaded in webBrowser control
if (!willNavigate)
{
willNavigate = true;
return;
}
// cancel navigation to the clicked link in the webBrowser control
e.Cancel = true;
var startInfo = new ProcessStartInfo
{
FileName = e.Uri.ToString()
};
Process.Start(startInfo);
}
}
}
@MainWindow.xaml
<Window x:Class="OpenDefaultBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="464" Width="1046">
<Grid>
<WebBrowser Height="425" HorizontalAlignment="Left" Name="webBrowser1" VerticalAlignment="Top" Width="1024" Source="http://stackoverflow.com/" Navigating="webBrowser1_Navigating" />
</Grid>
</Window>
Opening same origin page in default browser from outlook add-in
The behavior that you're seeing is by design. New windows are opened with Edge if it is the same domain in order to support using cookies to pass data to the window.
Related Topics
How to Add a Border to the Top and Bottom of an Android View
How to Add a Button Dynamically in Android
How to Get the Absolute Coordinates of a View
Android Starting Service At Boot Time , How to Restart Service Class After Device Reboot
Text Size and Different Android Screen Sizes
Android: How to Bind Spinner to Custom Object List
Include .So Library in APK in Android Studio
Android Material Design Button Styles
How to Remove Application from Recent Application List
How to Change Spinner Text Size and Text Color
Adb Android Device Unauthorized
Java.Lang.Runtimeexception: Unable to Instantiate Activity Componentinfo
Android: How to Check If a View Inside of Scrollview Is Visible
How to Change the Floating Label Color of Textinputlayout
Android: Storing Username and Password