Joseph Dindinger
Posts: 0
Joined: Sat Nov 30, 2013 12:27 am

JavaScript Crashing my App!

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

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

JavaScript Crashing my App!

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://

Joseph Dindinger
Posts: 0
Joined: Sat Nov 30, 2013 12:27 am

JavaScript Crashing my App!

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?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

JavaScript Crashing my App!

Yes, that's also an option.

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

Joseph Dindinger
Posts: 0
Joined: Sat Nov 30, 2013 12:27 am

JavaScript Crashing my App!

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?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

JavaScript Crashing my App!

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

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

JavaScript Crashing my App!

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

Joseph Dindinger
Posts: 0
Joined: Sat Nov 30, 2013 12:27 am

JavaScript Crashing my App!

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

JavaScript Crashing my App!

Hi Joseph,

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

Return to “Issues”