Skip to content

Commit 0ce00c6

Browse files
authored
Convert a jQuery script to Web API (#1062)
* Eliminating early exit test as bootstrap/DOM may return `b` tag, a.k.a child tag, instead of `a` tag... unreachable code usually * Refining target tags detected * Works in Opera Presto *(Oldest supported browser. Certain older versions of IE are unsupported already with jQuery)* * Comment up the code a bit Applies to #904
1 parent d8c1f1d commit 0ce00c6

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed
+37-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
11
<script type="text/javascript">
22
(function () {
3+
function onClick(aEv) {
4+
var container = aEv.target;
5+
var anchor = null;
6+
var url = null;
37

4-
$(document).ready(function () {
5-
// https://stackoverflow.com/questions/4904938/link-entire-table-row
6-
7-
$('.tr-link').click(function (aE) {
8-
var anchor = null;
9-
var url = null;
10-
11-
if ($(aE.target).is('a,input,a *')) { // anything else you don't want to trigger the click
8+
// Traverse up the DOM to find the container tr tag
9+
while (!container.classList.contains('tr-link')) {
10+
if (!(container = container.parentNode)) {
1211
return;
1312
}
13+
}
1414

15-
anchor = $(this).find('a.tr-link-a').first();
16-
if (!anchor) {
17-
return;
18-
}
15+
// Find the anchor tag for redirection
16+
anchor = container.querySelector('a.tr-link-a');
17+
if (!anchor) {
18+
return;
19+
}
1920

20-
url = anchor.attr('href');
21-
if (!url) {
22-
return;
23-
}
21+
// Check to see if the href tag exists
22+
url = anchor.getAttribute('href');
23+
if (!url) {
24+
return;
25+
}
26+
27+
// Stop propagation as soon as possible
28+
aEv.stopPropagation();
29+
30+
// Redirect to the anchor href
31+
window.location = url;
32+
}
33+
34+
function onDOMContentLoaded(aEv) {
35+
var i = undefined;
36+
var el = undefined;
37+
var els = document.querySelectorAll('tr.tr-link');
38+
39+
// Place handler on everything within the tr.tr-link
40+
for (i = 0, el; el = els[i++];) {
41+
el.addEventListener('click', onClick);
42+
}
43+
}
2444

25-
window.location = url;
26-
aE.stopPropagation();
27-
})
28-
});
45+
document.addEventListener('DOMContentLoaded', onDOMContentLoaded);
2946

3047
})();
3148
</script>

0 commit comments

Comments
 (0)