window.location.href not working in deviceready-Event on iPhone

3,392 views
Skip to first unread message

TdkBacke

unread,
Jul 7, 2010, 11:14:46 AM7/7/10
to phonegap
Please consider the following minimal example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></
script>

<script type="text/javascript" charset="utf-8">
window.onload = function()
{
document.addEventListener("deviceready", foo, true);
// window.location.href = 'next.html';
}

function foo()
{
alert('foo()'); // test if foo gets executed - always positive
window.location.href = 'next.html';
}
</script>
</head>
<body>
If you can see me, it's not working.
</body>
</html>

This code does not redirect to "next.html" on iPhone OS 3.1.3 as I
would expect. (Using "Phonegap.addConstructor" instead of the
"deviceready"-Event does not change anything.)
The redirection works if ...
1) exactly the same code is run in the Android Emulator (2.1 and 2.2)
2) the first window.location.href is uncommented

Can anyone tell me the reason for this behaviour? Is it some security
issue?

Giacomo Balli

unread,
Jul 7, 2010, 11:28:47 AM7/7/10
to phonegap
I believe there is a syntax error in

window.onload = function()
{
document.addEventListener("deviceready", foo,
true);
// window.location.href = 'next.html';
}

I would replace it with:

function start() {
document.addEventListener("deviceready", foo, true);
window.location.href = 'next.html';
}

and then

<body onLoad="start();">


let me know!
Reply all
Reply to author
Forward
0 new messages