I'm trying to get the example code in this SO question running on my system
getting the X/Y coordinates of a mouse click on an image with jQuery
It displays the position of the mouse on a click inside a div
. It works fine on Firefox
and Safari
but doesn't update on Chrome
and I'm wondering if I've done something wrong.
I'm using Google Chrome Version 23.0.1271.101
Here's the code I'm using.
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<p>Click to see the position!</p>
<div class="box" style="width: 200px; height: 200px; background: #F00"> </div>
<p id="position">Position will go here</p>
<script>
$(document).ready(function() {
$('.box').click(function(e) {
var offset = $(this).offset();
$('#position').html(Math.round(e.clientX - offset.left) + ", " + Math.round(e.clientY - offset.top));
});
});
</script>
</body>
</html>
Update:
Here is the error I see on Chrome's JavaScript console Uncaught ReferenceError: $ is not defined
but I'm not sure what to make of it.
Update2:
I cleared the browser cache and reloaded the page while having the JavaScript console open and noticed an error I had not seen before
[blocked] The page at https://<mywebsite>/index.html ran insecure content from http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js.
Uncaught ReferenceError: $ is not defined
So, I downloaded jquery.min.js
and placed it in the same directory as index.html
and changed the loading of the script to
<script src="./jquery.min.js" type="text/javascript"></script>
It now works on all three browsers.... so for some reason Chrome was the only one that complained about the url for jquery.min.js being insecure. To compound the problem, it didn't give me that error when I went into the JavaScript console, and I didn't see it until I had cleared the cache and I had the JavaScript console open.