Carl Wilson
Carl Wilson

Reputation: 189

Window.Open not working from within Function on Phonegap

I'm attempting to use a QR code scanner plugin for a project I'm working on, basically I'm modifying the example posted below so that instead of just scanning the code and outputting the string value to the page, I actually want it to physically open the link using the InAppBrowser.

Now whilst the function I've added fires (as far as I can tell) the InAppBrowser doesn't get invoked, however if I click on a link pre-embedded in the index page after trying a scan, it briefly shows the page I had tried to load via scanning before then loading the contents of the pre-embedding link (if that makes sense).

Original Demo https://github.com/wildabeast/BarcodeDemo

My Fork https://github.com/desrat/BarcodeDemo

Any help would be appreciated.

EDIT: Jonus Solution works great, but what if I wanted to move the function out of the alert callback and just open the browser immediately?

I already tried re-placing the alert with

namedFunc(result.text);

and

function(){namedFunc(result.text);};

Upvotes: 0

Views: 391

Answers (1)

jonas
jonas

Reputation: 935

When you pass namedFunc(result.text) as callback, the function is invoked immediately and actually its result (undefined) is passed.

Try:

navigator.notification.alert(result.text, namedFunc.bind(null, result.text), 'Scan Result', 'ok')

Or:

navigator.notification.alert(result.text, function() {namedFunc(result.text);}, 'Scan Result', 'ok')

UPDATE:

Your second question is hard to answer. Using namedFunc(result.text); should be right. After some testing (with iOS) it seems to me, that the InAppBrowser is opened but not shown, because I can inspect the opened website with Safari. This is quite strange and I have no idea what the reason is. Maybe it has something to do with the closing barcode scanner.

However you can fix it by using a timeout:

window.setTimeout(namedFunc.bind(null, result.text), 1000);

or maybe you prefer:

window.setTimeout(function() {namedFunc(result.text);}, 1000);

This is surely not a really good solution because the user has to wait a second before the browser opens and I can't even guarantee that one second is always enough (e.g. on slower devices), so it's a bit risky.

Upvotes: 1

Related Questions