Now that I've used it a lot, I think I'm starting to get the hang of jQuery and its capabilities. One thing I'm still unclear about is how global variables are initiated and used, and when the variable is in reference to a jQuery object, both cases especially in and with external JavaScript files.
For example, if we had this test website:
HTML file
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="someCSSreset.css">
<link rel="stylesheet" type="text/css" href="external.css">
<style>
.header {
color: orange;
background: blue;
font-size: 1.5em;
}
.content {
color: purple;
background: pink;
}
h1, h2 {
position: relative;
margin: 0 auto;
}
</style>
<script src="jQuery.min.js"></script>
<script src="example.js"></script>
<script>
// Global undefined variable. Is this allowed?
window.globalUndefined;
// Global variable referencing a number?
window.globalNumber = 42;
// Global undefined jQuery object?
window.$someElement;
// Global jQuery object. Do I need window. in front of the right-hand statement?
window.$allP = $("p");
$(document).ready(function() {
// Using global variable in document
$allP.click(function() {
$(this).css("color", "red");
});
// Using global variable from external file internally. Is this correct?
$allHeaders.click(function() {
$(this).css("color", "green");
});
// Am I passing these correctly?
doSomething($allP, $allHeaders);
});
</script>
</head>
<body>
<div class="header wrap">
<div class="header main">
<h1>EXAMPLE PAGE</h1>
<p>hi guys lol</p>
</div>
</div>
<div class="content wrap">
<div class="content main">
<h2>Paragraph 1</h2>
<p>Lorem ipsum</p>
</div>
</div>
</body>
</html>
And then, our two relevant external files:
external.css
body {
background-color: light-blue;
font-family: sans-serif;
width: 100%;
}
.wrap {
width: 50%;
border: 2px solid black;
}
example.js
// Does an external jQuery file need $(document).ready wrappers around it?
// Global undefined variable. Is this allowed?
window.extGlobalVar;
//Global variable referencing a number?
window.extGlobalNumber = 117;
// Global undefined jQuery object?
window.$extElementVar;
// Global jQuery object. Do I need a window. in front of the right-hand statement?
window.$allHeaders = $("h1");
function doSomething(p, h) {
$(p).mouseOver(function() {
// Using the internal global number variable?
$(this).css("width", globalNumber + "px");
});
// Using internal global jQuery
$allHeaders.mouseOver(function() {
// Using the external global number variable?
$(this).css("width", extGlobalNumber + "px");
});
// Can I later set the two undefined jQuery objects in the external file, or do I
// have to do it internally? Or does either work?
$someElement = $(".wrap");
$extElementVar = $(".main");
}
That's a lot of questions randomly strewn throughout some shitty code, so I'll reiterate them here:
- In the above examples, am I using the various global variables and global jQuery objects correctly (in both the internal and external cases)?
- In the above examples, am I passing the two global variables into that (externally-held) jQuery function correctly (in both the internal and external cases)?
- Am I calling that (externally-held) jQuery function correctly?
- Is there a way to be able to call an externally-held jQuery function on an object internally? E.g.
$("div").doSomething()
, butdoSomething
is defined in an external file. If so, how? - If we're allowed to have undefined global variables, can I later set them in the external file, or do they have to be set internally? Or can I do either?
- Can you declare global variables in a function or in the
$(document).ready()
wrapper if you format it aswindow.$(etc)
?