$ ("# Test") becomes Object instead of jQuery in Safari
I have javascript that works in most browsers other than Safari on Ipad, Iphone, or Safari on Windows.
LobbyMenuGames becomes a jQuery object for IE, Firefox and Chrome desktop browsers. I receive this message.
1 http://www.nurgi.com/atsso/working.png
But if I try to open in Safari I get this message.
1 http://www.nurgi.com/atsso/safari.png
The problem is that Safari fires $ (document) .ready (function () prematurely, but I'm not sure. I can get the variable as a jQuery object if I declare it again in the onclick event.
You can duplicate the same error by going to http://nurgi.com/atsso/test.aspx on your iphone.
This is simplified code with the additional code removed:
var LobbyMenuGames; // Global Variable
$(document).ready(function ()
{
if (LobbyMenuGames == null)
LobbyMenuGames = $("#LobbyMenuGames"); // <-- This becomes an Object instead of jQuery object
TestIsObj("OnReady After");
});
function TestIsObj(loc)
{
var nl = "\n";
var s = loc + nl;
if (LobbyMenuGames instanceof jQuery)
s = s + " IS jQuery" + nl;
else
s = s + " NOT jQuery" + nl;
if (LobbyMenuGames instanceof Object)
s = s + " IS Object" + nl;
else
s = s + " NOT Object" + nl;
alert(s);
}
source to share
null
is object
in Javascript, but your initial value is actually undefined
. This undefined
matching behavior is null
most likely browser dependent.
Try a simpler one !LobbyMenuGames
, since either the original state ( null
, or undefined
) is false:
var LobbyMenuGames; // Global Variable
$(document).ready(function ()
{
if (!LobbyMenuGames)
LobbyMenuGames = $("#LobbyMenuGames");
}
Or better yet, make sure it is always a jQuery object using an empty jQuery object:
var LobbyMenuGames = $(); // Global Variable is an empty jQuery collection
$(document).ready(function ()
{
LobbyMenuGames = $("#LobbyMenuGames");
}
This second method is handy for preventing false validations on jQuery objects.
source to share