-
Notifications
You must be signed in to change notification settings - Fork 480
splits into two tutorials for setup and fundamentals #661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<script>{ | ||
"title": "How To Setup jQuery", | ||
"level": "beginner" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This are two spaces. Use tabs please :) |
||
}</script> | ||
|
||
### jQuery: Setup | ||
|
||
This is a basic tutorial, designed to help you get started using jQuery. Start by creating the following HTML page and name it `index.html`, and open it in the browser: | ||
|
||
``` | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Demo</title> | ||
</head> | ||
<body> | ||
<a href="http://jquery.com/">jQuery</a> | ||
<script src="jquery.js"></script> | ||
<script src="demo.js"></script> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't indent inside of |
||
</body> | ||
</html> | ||
``` | ||
|
||
The `src` attribute in the first `<script>` element must point to a copy of jQuery. Download a copy of jQuery from the [Downloading jQuery](http://jquery.com/download/) page and store the `jquery.js` file in the same directory as your HTML file. | ||
|
||
*Note: When you download jQuery, the file name may contain a version number, e.g., `jquery-x.y.z.js`. Make sure to either rename this file to `jquery.js` or update the `src` attribute of the `<script>` element to match the file name.* | ||
|
||
Create another file in the same directory called `demo.js`. The `src` attribute in the second `<script>` element must point to the location of this JavaScript file, which will contain the code that will run on the page. | ||
|
||
### Launching Code on Document Ready | ||
|
||
To run code as soon as the [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction) is loaded and ready to be manipulated, jQuery has a statement known as the [ready event](http://api.jquery.com/ready/): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may of been correct to call it an event earlier, but anymore we should probably call this a "ready method" $( doucment ).ready( function {
// Your code here.
});
// Or the shorthand:
$(function() {
// document is ready!
}); |
||
|
||
``` | ||
$( document ).ready(function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We recently did some updates to the code style guide: $( document ).ready( function() {
} ); Notice the spacing. |
||
|
||
// Your code here. | ||
|
||
}); | ||
``` | ||
|
||
For example, inside the `ready` event, you can add a click handler to the link: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/event/callback/ ? |
||
|
||
``` | ||
$( document ).ready(function() { | ||
|
||
$( "a" ).on( "click", function popUp( event ) { | ||
|
||
alert( "Thanks for visiting!" ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the double spacing here. I'm also torn about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should totally discuss an article on debuggers but for the time being I think alerts are fine. |
||
|
||
}); | ||
|
||
}); | ||
``` | ||
|
||
Copy the above jQuery code into your JavaScript file. Then, save your HTML file and reload the demo page in your browser. Clicking the link should now first display an alert pop-up, then continue with the browser's default behavior of navigating to http://jquery.com. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took many of them out especially in places where it's clearer to indicate a file by name - but the ones left sound natural and using "one" or the passive voice might sound awkward. Lemme know if you think they still need replacing. |
||
|
||
For `click` and most other [events](http://api.jquery.com/category/events/), you can prevent the default browser behavior by calling `event.preventDefault()` in the event handler: | ||
|
||
``` | ||
$( document ).ready(function() { | ||
|
||
$( "a" ).on( "click", function customResponse( event ) { | ||
|
||
alert( "As you can see, the link no longer took you to jquery.com" ); | ||
|
||
event.preventDefault(); | ||
|
||
}); | ||
|
||
}); | ||
``` | ||
|
||
Try replacing your first snippet of jQuery code, which you previously copied into your JavaScript file, with the one above. Save the HTML file and reload the page to try it out. | ||
|
||
###Alternative Setups | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
Commonly used shorthand for the `ready` event uses `$` to reference jQuery: | ||
|
||
``` | ||
$(function() { | ||
|
||
$( "a" ).on("click", function raiseAlert( event ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the double spacing, unless you think it adds readability |
||
|
||
alert( "Works interchangeably!" ); | ||
|
||
}); | ||
|
||
}); | ||
``` | ||
|
||
You may encounter a somewhat similar strategy of running the code in an `onload` event as opposed to using `ready`, as shown below. `onload` will wait for all assets such as images and external style sheets to be fully loaded, so it should only be used in cases where the JavaScript needs to reference values from those assets. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this even worth mentioning? |
||
|
||
``` | ||
window.onload = function waitForIt() { | ||
|
||
alert( "This will typically be slower than using .ready()" ); | ||
|
||
}; | ||
``` | ||
|
||
In other cases you may want to alias the jQuery namespace to avoid potential conflicts with other JavaScript libraries that may also use `$`. The `ready` function can take an argument, so you can pass it another function that takes the jQuery global object as its argument. This provides a "private" function scope in which you can safely reference jQuery by `$`. | ||
|
||
```jQuery( document ).ready(function setUp( $ ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need a newline after the "syntax fence" |
||
// Code using $ as usual goes here. | ||
}); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will want to handle a redirect for this I imagine. That should block this being deployed to live. I can handle landing together when this PR is ready.