onetrueshady
onetrueshady

Reputation: 173

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?

Upvotes: 1

Views: 1009

Answers (2)

onetrueshady
onetrueshady

Reputation: 173

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
}

Upvotes: 1

Or Assayag
Or Assayag

Reputation: 6336

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

Upvotes: 0

Related Questions