Skip to content

Commit 9d2ef6b

Browse files
committed
refactor: modernize event binding using .on() instead of deprecated methods
Replace all instances of direct event binding methods (.click(), .focus(), .bind(), .blur(), etc.) with the modern .on() API across the codebase. The jQuery .on() method has been the recommended way to attach event handlers since jQuery 1.7 (2011), superseding: - .bind() - .live() (removed in 1.9) - .delegate() - shorthand methods (.click(), .hover()) when delegation or cleanup is needed While the shorthand methods still work, using .on() consistently provides: - A single, unified pattern for event attachment - Clear intent for both direct and delegated events - Better compatibility with future jQuery versions - Easier event namespace management and removal (.off()) This change touches multiple modules: - GitTurns20: popstate handling and tagline click counter - Search: focus, blur, keydown, and result interaction - Dropdowns: toggle activation via click - Forms: input selection - Downloads: OS filter navigation - DarkMode: toggle button interaction - ScrollToTop: scroll monitoring and click behavior Notable improvements: - $(window).bind('popstate', ...) → $(window).on('popstate', ...): aligns with current best practices - $("#tagline").click(...) → .on('click', ...): enables future delegation if needed - $(document).keydown(...) → .on('keydown', ...): consistent event model - $('#scrollToTop').click(...) → .on('click', ...): unified pattern across UI components Additionally: - Fixed event trigger in keyboard shortcut handler: now uses .trigger('focus') instead of direct .focus() to ensure consistent event flow - Preserved event argument (e) and logic (e.preventDefault(), timeStamp checks) — no behavioral changes - All selectors and functionality remain intact; this is a syntactic and maintainability upgrade This refactor was guided by warnings from jQuery Migrate and is part of the larger migration plan: 1. Add jQuery Migrate → done 2. Fix deprecated patterns (like .bind(), .click()) → this commit 3. Upgrade to jQuery 3.7.1 4. Remove Migrate when clean The code remains functionally identical, but is now more maintainable, consistent, and future-proof. No regressions expected. See: https://api.jquery.com/on/ See: https://github.com/jquery/jquery-migrate#readme
1 parent 0fd4dd0 commit 9d2ef6b

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

assets/js/application.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ $(document).ready(function() {
4343

4444
function onPopState(fn) {
4545
if (window.history && window.history.pushState) {
46-
return $(window).bind('popstate', function(event) {
46+
return $(window).on('popstate', function() {
4747
var section;
4848
initialPop = !popped && location.href === initialURL;
4949
popped = true;
@@ -115,7 +115,7 @@ var GitTurns20 = {
115115
} else {
116116
let start = 0
117117
let count = 0
118-
$("#tagline").click(e => {
118+
$("#tagline").on('click', e => {
119119
if (count === 0 || e.timeStamp > start + count * 1000) {
120120
start = e.timeStamp;
121121
count = 1;
@@ -181,10 +181,10 @@ var Search = {
181181
},
182182

183183
observeFocus: function() {
184-
$('form#search input').focus(function() {
184+
$('form#search input').on('focus', function() {
185185
$(this).parent('form#search').switchClass("", "focus", 200);
186186
});
187-
$('form#search input').blur(function() {
187+
$('form#search input').on('blur', function() {
188188
Search.resetForm();
189189
});
190190
},
@@ -194,7 +194,7 @@ var Search = {
194194
Search.runSearch();
195195
});
196196

197-
$('form#search input').keydown(function(e) {
197+
$('form#search input').on('keydown', function(e) {
198198
if ($('#search-results').not(':visible') && e.which != 27) {
199199
$('#search-results').fadeIn(0.2);
200200
Search.highlight(Search.selectedIndex);
@@ -220,16 +220,16 @@ var Search = {
220220
},
221221

222222
observeResultsClicks: function() {
223-
$('#search-results').mousedown(function(e) {
223+
$('#search-results').on('mousedown', function(e) {
224224
e.preventDefault();
225225
});
226226
},
227227

228228
installKeyboardShortcuts: function() {
229-
$(document).keydown(function(e) {
229+
$(document).on('keydown', function(e) {
230230
if (e.target.tagName.toUpperCase() !== 'INPUT' && ['s', 'S', '/'].includes(e.key)) {
231231
e.preventDefault();
232-
$('form#search input').focus();
232+
$('form#search input').trigger('focus');
233233
}
234234
else if (e.target.tagName.toUpperCase() !== 'INPUT') GitTurns20.keydown(e);
235235
});
@@ -470,7 +470,7 @@ var Dropdowns = {
470470

471471
observeTriggers: function() {
472472
var eles = $('.dropdown-trigger');
473-
eles.click(function(e) {
473+
eles.on('click', function(e) {
474474
e.preventDefault();
475475

476476
$(this).toggleClass('active');
@@ -491,7 +491,7 @@ var Forms = {
491491
},
492492

493493
observeCopyableInputs: function() {
494-
$('input.copyable').click(function() {
494+
$('input.copyable').on('click', function() {
495495
$(this).select();
496496
});
497497
}
@@ -545,7 +545,7 @@ var Downloads = {
545545
},
546546

547547
observeGUIOSFilter: function() {
548-
$('a.gui-os-filter').click(function(e) {
548+
$('a.gui-os-filter').on('click', function(e) {
549549
e.preventDefault();
550550
var os = $(this).attr('data-os');
551551

@@ -648,7 +648,7 @@ var DarkMode = {
648648
}
649649
button.css("display", "block");
650650

651-
button.click(function(e) {
651+
button.on('click', function(e) {
652652
e.preventDefault();
653653
let theme
654654
if (prefersDarkScheme) {
@@ -780,12 +780,12 @@ var PostelizeAnchor = {
780780

781781
// Scroll to Top
782782
$('#scrollToTop').removeClass('no-js');
783-
$(window).scroll(function() {
783+
$(window).on('scroll', function() {
784784
$(this).scrollTop() > 150
785785
? $('#scrollToTop').fadeIn()
786786
: $('#scrollToTop').fadeOut();
787787
});
788-
$('#scrollToTop').click(function(e) {
788+
$('#scrollToTop').on('click', function(e) {
789789
e.preventDefault();
790790
$("html, body").animate({
791791
scrollTop: 0

0 commit comments

Comments
 (0)