I am using jQuery mobile with the Google Map API. The map does not display properly if I do not refresh the page, and I do not understand why.
here is my map.html file:
<div data-role="page" id="map-page"> <div data-role="content" id="canvas-map" style="background-color:red"></div> <script src="js/map.js"></script> <script type="text/javascript"> var $canvasMap = $('#canvas-map'); $('#canvas-map').on('pagebeforeshow', initMap()); </script> </div>
and my map.js file:
function initMap() { "use strict"; google.maps.visualRefresh = true; var marker, map, myLocation = {lat:50, lon:-80}, mapOptions = { zoom: 5, center: new google.maps.LatLng(myLocation.lat, myLocation.lon), mapTypeId: google.maps.MapTypeId.PLAN, disableDefaultUI: true }; $('#canvas-map').css("height", "200px").css("padding", "0px"); map = new google.maps.Map(document.getElementById('canvas-map'), mapOptions); marker = new google.maps.Marker({ map: map, draggable: false, animation: google.maps.Animation.DROP, position: new google.maps.LatLng(myLocation.lat, myLocation.lon), }); }
If I go from another page to the previous page, I get the following result: 
And then, when I update, I get the expected result: 
This is obviously not a problem with the canvas, as it is displayed (in red). In addition, if I move around the map, I see a marker displayed in the correct position. These screenshots were taken using Google Chrome, I tried with Firefox, and the canvas here is completely red, with no map at all.
PS: this is a simple version of my source code, but the behavior is the same.
EDIT:
See Gajotres answer for more details, but basically, in the link to access the map.html page, adding target="_blank" solved the problem. Note that target="_self" seems to work just as weird as it should be the default. Read more about target here .