简体   繁体   中英

How to open a url in Safari instead of the default browser (JavaScript)?

I have loaded an html file in a webkit view inside my app. The html file acts like a search engine and has the following code which gets triggered when the users hits the search action:

function search() {
    var input = document.getElementById("search_form_input_homepage").value;
    window.open("https://duckduckgo.com/?q=" + input);
}

However, I need the result url to open in Safari. As of now, window.open loads up the url in the default browser. How do I achieve this?

I found this link that talks about different url schemes in Safari. But I am very new to JavaScript. This is in fact the first time I am working with JS. So I could not understand how to implement this. Can anybody help me with this, please?

I managed to do this using Swift. I added the following webkit delegate method.

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        NSWorkspace.shared.open([navigationAction.request.url!], withAppBundleIdentifier: "com.apple.safari", options: .default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
    return nil
}

You can't control which browser to open via JavaScript code, it's related to the operating system / user default selection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM