Page 1 of 1

JavaScript Crashing my App!

Posted: Wed Dec 18, 2013 10:18 pm
by Joseph Dindinger

I have an app that is almost ready, but I've found that with specific JavaScript, my app is crashing.

The following code works fine if _urlweb equals "http://www.google.com":

prevar theURL = localStorage.getItem("urlweb");
window.open(theURL, 'blank', 'location=yes');/pre

But I get a 404 error if the _urlweb variable equals "www.google.com".

I cannot always count on the user entering the http:// before the URL, so I wrote this simple code, but when I try to use it, it crashes the whole app (doesn't even run at all.)

prevar theURL = localStorage.getItem("urlweb");
if (theURL !== null && theURL !== "") {
If(theURL.substring(0,4) !== "http") {
theURL = "http://" + theURL;
}
window.open(theURL, 'blank', 'location=yes');
}/pre

Can anybody see why this would be happening?

Thanks!

Joseph


JavaScript Crashing my App!

Posted: Wed Dec 18, 2013 11:38 pm
by maxkatz

Is this a PhoneGap app? It's probably how PhoneGap works (or how the current version handles it). You can add validation and if http:// is not present, update the value with http://


JavaScript Crashing my App!

Posted: Wed Dec 18, 2013 11:48 pm
by Joseph Dindinger

Well, unfortunately it is pulling the value from an outside database, the user is not entering it in the app. Or are you saying I do validation when the value is being pulled in via the service?


JavaScript Crashing my App!

Posted: Wed Dec 18, 2013 11:50 pm
by maxkatz

Yes, that's also an option.

You can also check PhoneGap forums to see if there is another solution to this issue.


JavaScript Crashing my App!

Posted: Thu Dec 19, 2013 12:19 am
by Joseph Dindinger

I'm actually not sure that it is PhoneGap. So far, I'm only building it in HTML5, but have not yet pushed out to PhoneGap or exported for the iPhone or anything like that. If I'm building in HTML5 can that still be a PhoneGap issue?


JavaScript Crashing my App!

Posted: Thu Dec 19, 2013 12:26 am
by maxkatz

Then it's the browser that doesn't like that.


JavaScript Crashing my App!

Posted: Thu Dec 19, 2013 3:28 am
by Illya Stepanov

Hi Joseph,

Should work in the browser - I've tested, try this one:

pre
var theURL = localStorage.getItem("_urlweb");

if (theURL !== '' && theURL !== null)
{
if (theURL.substring(0,4) !== "http") {
theURL = "http://" + theURL;
}
window.open(theURL, '_blank', 'location=yes');
}
/pre


JavaScript Crashing my App!

Posted: Thu Dec 19, 2013 5:56 pm
by Joseph Dindinger

Thanks Illya!

I believe it was the " instead of '.

I made a few changes to catch the null value and now it it working just fine. Here is the final version.

prevar theURL = localStorage.getItem("urlweb");
if (theURL !== '' && theURL !== null && theURL !== 'null')
{
if (theURL.substring(0,4) !== 'http') {
theURL = 'http://' + theURL;
}
window.open(theURL, 'blank', 'location=yes');
}/pre


JavaScript Crashing my App!

Posted: Thu Dec 19, 2013 6:25 pm
by Kateryna Grynko

Hi Joseph,

Thank you for the update! Glad it's working.