So I’m doing some basic href functionality replacement for users that have JS enabled. I admit I’m more of a PHP guy so I may have some syntax issues in my JQuery, please point those issues out.
This will replace all my href attributes in my nav links to remove their functionality and then replace with an click event listener…
function replaceLinks() {
$('#nav_ribbon li a').each(function() {
$(this).attr({'href': 'javascript:void(0)'}).on('click', navLinkClick(this));
});
}
function navLinkClick(selector) {
var pageValue = $(selector).attr('title').toLowerCase();
var page = pageValue + '.php';
$('#content').load(page);
}
So on document ready I run replaceLinks but when my page loads it automatically activates the handler without the event taking place… so I end up with loaded content instead of the home page content
And although what you proposed will fix the problem, there are also other ways to improve the code quality, such as removing the “javascript:void(0)” thing (event.preventDefault is cleaner), and passing a reference to navLinkClick itself rather than wrapping it in an anonymous function.
function replaceLinks() {
$('#nav_ribbon li a').each(function() {
[COLOR="#FF0000"]$(this).on('click', navLinkClick);[/COLOR]
});
}
function navLinkClick([COLOR="#FF0000"]event[/COLOR]) {
[COLOR="#FF0000"]event.preventDefault();[/COLOR]
var pageValue = $([COLOR="#FF0000"]event.target[/COLOR]).attr('title').toLowerCase();
var page = pageValue + '.php';
$('#content').load(page);
}