Clicking Urls Opens Default Browser

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



Leave a reply



Submit