2

I am trying to build app with WebView and click/tap events on URL-s inside WebView. Solution below opens external browser and URL but it loads same url content in webview as well. Is there a way to prevent loading new url inside webview?

Here is my code sample.

function onWebViewLoaded(webargs) {
const page = webargs.object.page;
const vm = page.bindingContext;
const webview = webargs.object;


webview.on(webViewModule.WebView.loadFinishedEvent, (args) => {
   let message = "Loading in progress....";
   if (!args.error) {
    message = `WebView loading finished with url: ${args.url}`;
} else {
    message = `Error received ${args.url} : ${args.error}`;
}


   if (args.url.indexOf('http://') === 0) {
    // Stop the loading url first... but how..

    // Open URL in external default browser
    utilityModule.openUrl(args.url);
}
});
}

I have tried with setting a flag isUserInteractionEnabled="false" added to my xml view but then all interactions are disabled. Does someone knows how to do this?

1 Answer 1

3

Try with loadStartedEvent

    webview.on(webViewModule.WebView.loadStartedEvent, (args) => {
        if(!args.url.includes("whatever.net")){
            webview.stopLoading();
            utilsModule.openUrl(args.url);
        }
    });

This way we catch the loadStartedEvent instead of loadFinishedEvent (where the url is already loaded), we check the url if we want to filter depending on it, and here we can webview.stopLoading() for stop the page load.

Sign up to request clarification or add additional context in comments.

4 Comments

first you should ask that what does he want to do, It looks like a logic game questions, not Nativescript... 1. Why does he need actually? 2. What does he need actually? 3. Why doesn't he open that in Webview? so this app aims only app2app routing? (like a desktop webpage shortcut). If he plans to publish it on stores i think probably AppStoreConnect will reject his app, maybe PlayStore too...
@DrAchernar I can't see the problem, he wants to open url's in external browser but not in webview, myself have a published app with this behavior without problem.
@HenryWoody I find the answer self explanatory, we catch the loadStartedEvent instead of loadFinishedEvent (here the url is already loaded), we check the url if we want to filter depending on it, and here we can webview.stopLoading() for stop the page loading proccess.
@Maka please accept my answer if you think it's ok for your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.