Page 1 of 1
WebView with links and target="_blank"
Posted: Fri Nov 14, 2025 11:11 am
by Kukulkan
Hi,
The following code illustrates my problem (I'm on Linux with PB6.21):
Code: Select all
OpenWindow(0, 100, 100, 400, 400, "Test", #PB_Window_SystemMenu)
WebViewGadget(0, 0, 0, 400, 400)
SetGadgetItemText(0, #PB_WebView_HtmlCode,
~"<a href=\"https://www.google.com\" target=\"_blank\">https://www.google.com _blank</a><br>"+
~"<a href=\"https://www.google.com\" target=\"_self\">https://www.google.com _self</a>")
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
If I click the link with
_self, it works while the link with
_blank just does nothing. I like to have a chance to handle this event so I can handle this by myself (eg open system webbrowser) but without editing or changing or injecting to html.
I know that this is likely for safety, but on Android and Linux with WebViews (Kotlin and Swift) such links open up in system webbrowser. I like to have similar behavior on Windows/Linux/MacOS.
Is there a way I can handle or enable this
without having to intercept the link on HTML side to run a bound function?
Re: WebView with links and target="_blank"
Posted: Sun Nov 16, 2025 8:14 pm
by normeus
On windows PB 6.12x64 and 6.21x64, webviewgadget() opens up a window with an instance of webview2, not the default browser.
I have not used the webviewgadget() so I have no idea if opening a new window like a regular browser would be a desired behavior.
Norm.
Re: WebView with links and target="_blank"
Posted: Mon Nov 17, 2025 8:15 am
by Kukulkan
Thanks for testing. I'm basically looking for a way to intercept this and handle it by myself. I already have cross platform functions for correctly handling URIs and I like to use this if someone clicks such a link.
Re: WebView with links and target="_blank"
Posted: Thu Nov 20, 2025 10:47 am
by Kukulkan
So, no one has a solution for this? Links with target="_blank" are not supported and do not work in WebViewGadget or do undefined things like opening another WebView instance?
If I have control over the website, I may be able to intercept these links and run a local bound function call. But if the content is not by me (eg some external website), it is not fully functional

Re: WebView with links and target="_blank"
Posted: Thu Nov 20, 2025 10:24 pm
by Justin
For linux:
Code: Select all
EnableExplicit
ImportC ""
webkit_policy_decision_use(decision.i)
webkit_policy_decision_ignore(decision.i)
webkit_policy_decision_download(decision.i)
webkit_navigation_policy_decision_get_navigation_action.i(decision.i)
webkit_navigation_action_get_request.i(action.i)
webkit_uri_request_get_uri.i(req.i)
EndImport
Enumeration
#WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION
#WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION
#WEBKIT_POLICY_DECISION_TYPE_RESPONSE
EndEnumeration
ProcedureC.l decide_policy_cb(webview.i, decision.i, type.l, gdt.i)
Protected.i action, req, uri
Protected.s suri
If type = #WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION
action = webkit_navigation_policy_decision_get_navigation_action(decision)
req = webkit_navigation_action_get_request(action)
uri = webkit_uri_request_get_uri(req)
If uri
suri = PeekS(uri, -1, #PB_UTF8)
EndIf
Debug suri
ElseIf type = #WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION
Debug "_blank"
EndIf
webkit_policy_decision_use(decision)
;webkit_policy_decision_ignore(decision) ;Block
EndProcedure
Procedure webview_init(gdt.i)
Protected.i webview
webview = gtk_bin_get_child_(GadgetID(gdt))
g_signal_connect_(webview, "decide-policy", @decide_policy_cb(), gdt)
EndProcedure
Procedure main()
Protected.i Event, wv
OpenWindow(0, 100, 100, 400, 400, "Test", #PB_Window_SystemMenu)
wv = WebViewGadget(#PB_Any, 0, 0, 400, 400)
webview_init(wv)
SetGadgetItemText(wv, #PB_WebView_HtmlCode,
~"<a href=\"https://www.google.com\" target=\"_blank\">https://www.google.com _blank</a><br>"+
~"<a href=\"https://www.google.com\" target=\"_self\">https://www.google.com _self</a>")
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndProcedure
main()
Re: WebView with links and target="_blank"
Posted: Thu Nov 20, 2025 10:45 pm
by Justin
Or better like this to get also the uri of the new window:
Code: Select all
EnableExplicit
ImportC ""
webkit_policy_decision_use(decision.i)
webkit_policy_decision_ignore(decision.i)
webkit_policy_decision_download(decision.i)
webkit_navigation_policy_decision_get_navigation_action.i(decision.i)
webkit_navigation_action_get_request.i(action.i)
webkit_uri_request_get_uri.i(req.i)
EndImport
Enumeration
#WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION
#WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION
#WEBKIT_POLICY_DECISION_TYPE_RESPONSE
EndEnumeration
ProcedureC.l decide_policy_cb(webview.i, decision.i, type.l, gdt.i)
Protected.i action, req, uri
Protected.s suri
If type = #WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION Or type = #WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION
action = webkit_navigation_policy_decision_get_navigation_action(decision)
req = webkit_navigation_action_get_request(action)
uri = webkit_uri_request_get_uri(req)
If uri
suri = PeekS(uri, -1, #PB_UTF8)
EndIf
Debug suri
If type = #WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION
Debug "_blank"
ElseIf type = #WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION
Debug "_self"
EndIf
EndIf
webkit_policy_decision_use(decision)
;webkit_policy_decision_ignore(decision) ;Block
EndProcedure
Procedure webview_init(gdt.i)
Protected.i webview
webview = gtk_bin_get_child_(GadgetID(gdt))
g_signal_connect_(webview, "decide-policy", @decide_policy_cb(), gdt)
EndProcedure
Procedure main()
Protected.i Event, wv
OpenWindow(0, 100, 100, 400, 400, "Test", #PB_Window_SystemMenu)
wv = WebViewGadget(#PB_Any, 0, 0, 400, 400)
webview_init(wv)
SetGadgetItemText(wv, #PB_WebView_HtmlCode,
~"<a href=\"https://www.google.com\" target=\"_blank\">https://www.google.com _blank</a><br>"+
~"<a href=\"https://www.google.com\" target=\"_self\">https://www.google.com _self</a>")
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndProcedure
main()
Re: WebView with links and target="_blank"
Posted: Fri Nov 21, 2025 9:20 am
by Kukulkan
Thank you Justin. I yesterday solved it by injecting script to catch links on a[target="_blank"] and call a bound local function. Not elegant, but working for my needs.
Not bad to have your solution on the forum here, so maybe other will profit from that, too.