patternjavascriptModerate
Advice needed for scopes in JavaScript
Viewed 0 times
neededjavascriptscopesadvicefor
Problem
I would like to connect this "JS" to Bugzilla (example: bugzilla.mozilla.org or landfill.bugzilla.org).
I started to learn JS language today and I would like to ask you:
```
quicksearch = document.getElementById('quicksearch_top');
comment = document.getElementById('comment');
severity = document.getElementById('bug_severity');
priority = document.getElementById('priority');
commit_top = document.getElementById('commit_top');
commit = document.getElementById('commit');
function focusonload() {
// may be it should be
// in next function?
function cursorfocus(s) {
x = window.scrollX;
y = window.scrollY;
s.focus();
window.scrollTo(x, y);
}
if (comment !== null) {
cursorfocus(comment)
} else {
quicksearch.focus();
}
}
function navigation(keypressed) {
keypressed = keypressed || window.event;
function selectelement(w, select) {
w.value = select;
}
keyCode = keypressed.keyCode || keypressed.which,
kn = {
enter: 13,
save: 83,
down: 40,
up: 38,
p1: 49,
p2: 50,
p3: 51,
p4: 52,
p5: 53,
};
if (keypressed.altKey) {
if (keyCode == nk.save && commit_top!==null){
commit_top.click();
}
var key_str;
if (priority!==null){
switch (keyCode) {
case kn.p1: key_str = "P1"; break;
case kn.p2: key_str = "P2"; break;
case kn.p3: key_str = "P3"; break;
case kn.p4: key_str = "P4"; break;
case kn.p5: key_str = "P5"; break;
}
}
selectelement(priority, key_str);
severity.focus();
} else if (keypressed.ctrlKey) {
switch (keyCode) {
case kn.enter:
if (commit!==null&&comment.value!=="") {
I started to learn JS language today and I would like to ask you:
- How can I not do bad things in global scope?
- How should I use functions (in
varor not)?
```
quicksearch = document.getElementById('quicksearch_top');
comment = document.getElementById('comment');
severity = document.getElementById('bug_severity');
priority = document.getElementById('priority');
commit_top = document.getElementById('commit_top');
commit = document.getElementById('commit');
function focusonload() {
// may be it should be
// in next function?
function cursorfocus(s) {
x = window.scrollX;
y = window.scrollY;
s.focus();
window.scrollTo(x, y);
}
if (comment !== null) {
cursorfocus(comment)
} else {
quicksearch.focus();
}
}
function navigation(keypressed) {
keypressed = keypressed || window.event;
function selectelement(w, select) {
w.value = select;
}
keyCode = keypressed.keyCode || keypressed.which,
kn = {
enter: 13,
save: 83,
down: 40,
up: 38,
p1: 49,
p2: 50,
p3: 51,
p4: 52,
p5: 53,
};
if (keypressed.altKey) {
if (keyCode == nk.save && commit_top!==null){
commit_top.click();
}
var key_str;
if (priority!==null){
switch (keyCode) {
case kn.p1: key_str = "P1"; break;
case kn.p2: key_str = "P2"; break;
case kn.p3: key_str = "P3"; break;
case kn.p4: key_str = "P4"; break;
case kn.p5: key_str = "P5"; break;
}
}
selectelement(priority, key_str);
severity.focus();
} else if (keypressed.ctrlKey) {
switch (keyCode) {
case kn.enter:
if (commit!==null&&comment.value!=="") {
Solution
How can I not mess up the global scope?
You can use an IIFE ( Immediately Invoked Function Expression ) to surround your code and then assign all your variables with
etc.etc
Your keycode map is very nice, keep it that way!
One last comment is that you should not use
Try
You can use an IIFE ( Immediately Invoked Function Expression ) to surround your code and then assign all your variables with
var commitTop- quicksearch -> quickSearch
- selectelement -> selectElement
etc.etc
Your keycode map is very nice, keep it that way!
One last comment is that you should not use
document.onkeydown = navigation;, there is a good chance that you will break something this way.Try
document.addEventListener("keydown", navigation, true); instead.Code Snippets
(function(){
var quicksearch = document.getElementById('quicksearch_top');
var comment = document.getElementById('comment');
var severity = document.getElementById('bug_severity');
var priority = document.getElementById('priority');
var commit_top = document.getElementById('commit_top');
var commit = document.getElementById('commit');
//the rest of your code here
}());Context
StackExchange Code Review Q#40462, answer score: 10
Revisions (0)
No revisions yet.