Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7072263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:47:14+00:00 2026-05-28T05:47:14+00:00

I have a WebView wherein I would like anchor tags with rel=external to open

  • 0

I have a WebView wherein I would like anchor tags with rel=external to open in the Android browser but all other links to stay in the WebView.

So the content will load within the WebView if the user taps a link whose markup looks like this:

<a href="http://example.com/">Whatever</a>

But the content will load in the Android browser if the user taps a link whose markup looks like this:

<a href="http://example.com/" rel="external">Whatever</a>

Here’s my relevant code (with one bit of pseudocode identified with a comment) in the WebViewClient code:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (! rel=external) {   //  <-- That condition...how do I do that?
        view.loadUrl(url);
        return false;
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
        return true;
    }

Would the best way to determine whether there is a rel=external attribute/value be to somehow use addJavascriptInterface() and have JavaScript inform Java whether or not there is a rel attribute and what the value is?

Or is there a better way?

(I am looking for a solution that does not involve checking the domain of the URL because there are an arbitrary number of domains that need to be treated as internal and that I cannot know in advance or determine easily on-the-fly.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:47:15+00:00Added an answer on May 28, 2026 at 5:47 am

    I think a variation of your original suggestion is probably best if you don’t want to modify the existing URL.

    Use addJavaScriptInterface to add a class to handle your external URL loading. An inner class might look like this:

    public class JavaScriptInterface {
        public void viewExternalUrl(String url) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
    

    And of course adding it:

    webView.addJavascriptInterface(new JavaScriptInterface(), "Android");
    

    Inject JavaScript to add click handlers to call your JavaScript interface after the page is loaded (using JQuery for simplicity’s sake) through loadUrl. You can detect the page load finishing by creating a custom WebViewClient and overriding onPageFinished.

    webView.setWebViewClient(new WebViewClient() {
        private static final String sJs = "javascript:" +
            Uri.encode("$('a[@rel$='external']').click(function(){ Android.viewExternalUrl($(this).attr('href')); });");
    
        @Override
        public void onPageFinished(WebView view, String url) {
            webView.loadUrl(sJs);
        }
    }
    

    I haven’t tested any of this code, but it should work, assuming you have JQuery loaded in the page. If not, you can probably come up with some JavaScript to manipulate the DOM without it or you can use loadUrl to manually load JQuery first before the loading the JavaScript.

    Note that this approach completely abandons any use of shouldOverrideUrlLoading

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webview which I would like to remove the elasticity from. As
I have a WebView. I'd like to show some page from my server, but
I have simple WebView code like this: WebView wv = (WebView) findViewById(R.id.webview1); wv.loadUrl(http://en.wikipedia.org/wiki/Book); But
I have a webview which basically is capable of intercepting all sorts of links,
I have a WebView that I'm using to open some files stored in the
I have a WebView app that includes embedded YouTube videos on a page. But
I have a WebView that loads a local HTML file like this: [webView loadRequest:[NSURLRequest
I have a webView and I use [NSURLRequest requestWithURL:] to load the page. But
I have a WebView with a WebViewClient with the onRecievedError method set. But if
I have WebView in myLayout and I am trying to load html string like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.