I have some javascript that changes a # hash URL and loads some HTML into an element using AJAX.
// Manually trigger a hashchange to start the app.
$(window).trigger('hashchange');
function makeContentView() {
document.location.hash = "content";
return false; // we don't want event propagation - just AJAX call
}
$(function () {
$(window).on('hashchange', function () {
// On every hash change the render function is called with the new hash.
// This is how the navigation of our app happens.
render(window.location.hash);
});
function render(url) {
// This function decides what type of page to show
// depending on the current url hash value.
switch (url) {
case "#home":
$("#AJAX_Parent").load("body/index_body.html #AJAX_Child");
break;
case "#grid":
$("#AJAX_Parent").load("body/grid_body.html #AJAX_Child", function () {
var contents = document.getElementsByClassName("thumbnailImage");
for (i = 0; i < contents.length; i++) {
// THIS FUNCTION GETS CALLED WITHOUT ME CLICKING
contents.item(i).onclick = makeContentView();
}
}); // makeContentView IS ALWAYS BEING CALLED IMMEDIATELY
... // More switch options
^ Note that in the above example, the "contents.item(i).onclick = makeContentView();" assigns the makeContentView function to the onclick handler of my page's thumbnail images, but this makeContentView function is triggered unconditionally.
I only want makeContentView to be called when I click on one of the 6 thumbnail images with class=thumbnailImage