Window.Open not working from within Function on Phonegap - javascript

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);};

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.

Related

Image load event not working on second page load

I have a rails 4.2 app (no turbolinks) where I want to wait for an image to load before doing something with js. I used the jquery load callback
$(image).load(function() {
// do something here
});
and expected everything to be good however it only works on a hard reload (command + shift + R on osx). If I navigate by clicking a link then the image load event never fires, the image doesn't show up on the page but the DOM says it's there. I've already figured out a workaround using a promise instead but I'm curious for some insight into why this strange behaviour could arise?

Open .html page within same window only once. File run locally

I have a html file that will be run locally using IE. I want it to function more like an app, it will not be published to a site, I'm only using IE to view it. The code that follows will provide basic functions to do simple calculations. I've not been able to successfully use the window.onload event to create a new window that removes the scroll bar, title bar, menu, etc without it looping. I'm not sure if an If statement or a while statement is best for testing if the page is already open to stop the loop and I'm having a hard time understanding the syntax of how to test if the window.onload already has the window open. I guess I'm looking for some guidance on setting this up or a reference easily understood by a beginner. Thank you.
I at present have two html files. The first, its only purpose in life is to trigger the second to load as I want it to show.
function openWindow()
{
window.open("CouchShifts.html", "", "status=no,toolbar=no,menubar=no,navigationbar=no,location=no,resizable=no,scrollbars=n, width=440,height=200'");
}
window.onload = openWindow()
window.close("test.html")
I had tried to incorporate this idea into the original html file without success. Any starts on how to better handle this so that when the standalone html file is double clicked from the desk top it open as specified above only once without looping?
It's not wise to check window is open or not in the same windows onLoad. You should check it rather in the event that actually opens the window.
Here are some solutions for this.
Check if window is already open window.open
https://stackoverflow.com/a/10467344/672455

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page.
For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth.
The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/
Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way.
Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided?
P.S.: On my real site the same occurs with Chrome. In the example fiddle Chrome seems to not show the same behaviour as Safari, though.
Edit: I also tried to have the printing button on a different tag than an a anchor, like span or button, but that didn't help with the problem.
Edit: I run into this problem with Safari 5.1.2 on Mac OS 10.6.8. In the jsfiddle example, the behavior shows as a white "flicker" that covers the whole browser after having clicked the print button and closing (either printing or aborting) the print dialogue.
Edit: Just started a bounty for this question, as I am still looking for an explanation for this browser behaviour. I am more than willing to give more details on the issue. From my experience on SO it is not adviseable to post links to online pages, however I feel the problem is really hard to reproduce. I think the "flicker & re-render" I experience with the posted jsfiddle is hopefully is result of the same problem.
Edit: As explained in the comments, opening a new window containing the current state of the application, then printing and closing that window, might be a fix, but I don't consider it a sufficient answer to the problem at hand.
If you can't figure a way to fix the problem you might get a quick fix this way:
var win=window.open() the same page you are on
then win.print() it, then win.close(). Hope this helps.
Sadly, I don't have a solution. but since I was able to reproduce this in Safari and Webkit with the fiddle, I thought I'll at least offer my observations:
The white-out doesn't happen if you hit Print dialog button right away. You need to wait few seconds before you hit Print to be able to see the white. On OSX 10.6.8 I see the threshold marked by appearance of the spinning beachball.
When the mouse stays stationary since the Print button hit, the page stays white until you move the mouse again (beachball still spinning).
Hope this will help somehow.
I'm not sure if this will fix your problem but i recently found a very good Jquery plugin to print only certain part of your webpage.
You should give it a try :) It has a some very cool parameters to customize it to your needs.
Take a look here.
Why does your page refresh? Here's my answer on that. the window.print() function will print the entire content of your page. So since i read that you only want to print a part of the page, I guess you are using some function to remove all unwanted content from the page for a very short moment, then you call the window.print() function and after that you put all orignal content again on the screen. This results in a very short flash that will look like your page is refreshing and will load all flash parts again.
I had the same problem yesterday and this jquery plugin really helped me out. I hope it will help you too.
Have a nice day!
Since it seems window.print() is not part of any standard, browsers are free to implement it in any way they choose.
The only work-around I can see, if you really have to leave the page as-is, would be to open another window and print that. Which is a whole other, possibly worse, can of worms.
Try this in your function:
document.write("<script type='text/javascript'>window.print()</script>");
I am referencing this stack:
After window.open, can't print in Safari for Mac
Update, Try:
document.writeln("<script type='text/javascript'>window.print()</script>");

Loading page and executing JS on it...from JS bookmarklet?

Essentially I'm trying to
navigate to a webpage
wait for that webpage to load
execute a JS function/alert/whatever on that page
all from a single bookmarklet. Is this possible? I can't seem to get onload to work for me, but that may be because of my own personal failings here.
The simplest way I found to do this without needing Greasemonkey or something similar is to write your JS so that it checks to see if it is on the appropriate page, and goes there if it isn't. If it is on the page, then it executes the JS/alert/whatever. You have to use the bookmarklet twice, but you just need one bookmarklet, and it may still be quicker/easier the user doing the clicking/whatevering him or herself. So the code would look like this:
if(this.document.location.href != "[url]") { //Are we on the page yet?
this.document.location.href = "[url]"; // If not, go there
}
else {
if (document.readyState === "complete") { //Wait for the page to finish loading
// DO STUFF
}
}
You want to install the Greasemonkey extension for Firefox. (or gm4ie for IE, or greasemetal for Chrome (PersonalizedWeb also works in a much simpler way for Chrome), greasekit for Safari, or user.js for Opera)
Greasemonkey lets you do exactly this... run a script automatically on every page load (you can choose what pages/sites it loads on)
Otherwise you will need to click your bookmarklet on every page load in order to run your script.
Given there's no better solution, I thought I'd toss out that Opera natively supports user scripts to run on every page load. From there, you could have the script check the current url, and run if on appropriate page.
See here for documentation
Another option is to call window.open(...), and use the window object to manipulate the window. It is also possible to navigate multiple pages this way.

How can I find the source of rogue Ajax requests on IE?

I've rewritten my family web site using JavaScript (JQuery) making Ajax calls to PHP on the back end. It's your standard "bunch of image thumbnails and one main image, and when you click on a thumbnail image the main image changes" kind of thing. Everything is working as expected when using Firefox, but on IE, when I click on a thumbnail, the main image changes to the one I clicked and then immediately changes back to the first one. I have tried MS Script Debugger to no avail; I set a breakpoint in the JavaScript code that starts the Ajax call, and when I click the thumbnail the breakpoint fires. Then I hit F5 and it continues but does not fire again. If I use Wireshark to watch the actual TCP packets over the network, I can see that we are definitely sending more than one request to the server. I cannot figure out where the second query (the one to revert back to the original image) comes from.
Any suggestions? One example of what I'm talking about is here.
Debugging through your site here's what it looks is happening:
After the first image is pocessed, the resize event is being thrown, so this code gets called:
$(window).bind("resize", function(){
ResizeWindow( 'nicholas-1' )
});
which as you know reloads your gallery. Now I can't tell you why this is occurring but this is where the trouble starts.
For future reference to debug this I used VS2008 to attach to IE. I then put a break in $ajax() at:
// Send the data
try {
xhr.send(s.data);
} catch(e) {
jQuery.handleError(s, xhr, null, e);
}
Then I simply hit F5 which is run for the first two ajax calls, then I open up the call stack window when I found the rogue ajax call and walked the call stack back up to the function I posted earlier.
Good luck.
You could use Fiddler, a free debugging proxy for Internet Explorer. It was a great help for me many times when I had to debug specific,server-related problems on IE.
Here is an Introduction to Fiddler on MSDN.
alt text http://i.msdn.microsoft.com/Bb250446.ie_introfiddler_fig04(en-us,VS.85).gif
IE is a piece of work, isn't it? Have you tried something like this?
var inProcess = 0;
function eventHandler() {
if (inProcess == 0) {
inProcess = 1;
// do stuff
setTimeout('inProcess = 0', 5000);
}
}
Cute kid, by the way.

Categories