-
Notifications
You must be signed in to change notification settings - Fork 479
Splitting intro into two tutorials #714
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
aedb19a
5ec5a7e
b568186
71f81e9
317a6a2
658f1a2
8dd680a
0731a06
b756340
381f62f
a3ae666
33dc80c
cdb84df
70a5aed
92711ce
56db1b1
e0006aa
8634d09
da532c7
ca1cd09
e2f2d10
91c86fe
b42014c
f5e88e8
08844e0
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,88 @@ | ||
<script>{ | ||
"title": "How To Setup jQuery", | ||
"level": "beginner" | ||
}</script> | ||
|
||
### jQuery: Setup | ||
|
||
This is a basic tutorial, designed to help you get started using jQuery. Start by creating the following HTML page named `index.html` and open it in the browser: | ||
|
||
``` | ||
<!doctype html> | ||
<html> | ||
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. Altought this is not the main focus of this page, I'm for adding |
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Demo</title> | ||
</head> | ||
<body> | ||
<a href="http://jquery.com/">jQuery</a> | ||
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 should use the HTTPS version of the website |
||
<script src="jquery.js"></script> | ||
<script src="demo.js"></script> | ||
</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 `index.html`. | ||
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 should use the HTTPS version of the URL. 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 can also suggest to use a CDN as an alternative. |
||
|
||
*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 method](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. I'd change this:
in
|
||
|
||
``` | ||
$( document ).ready( function() { | ||
// Your code here. | ||
} ); | ||
|
||
// Or the shorthand: | ||
$( function() { | ||
// Document is ready! | ||
} ); | ||
``` | ||
|
||
For example, inside the `ready` event, you can add a click handler to the link: | ||
|
||
``` | ||
$( document ).ready( function() { | ||
$( "a" ).on( "click", function popUp( 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. Should we use a named function? Also, I'd suggest to remove the event as we don't use it. |
||
alert( "Thanks for visiting!" ); | ||
} ); | ||
} ); | ||
``` | ||
|
||
Copy the above jQuery code into `demo.js`, then save `index.html` and reload the page. Clicking the link should now first display an alert pop-up, and 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. Again, let's use the HTTPS version of the website. 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. Shouldn't this say save |
||
|
||
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: | ||
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. HTTPS URL |
||
|
||
``` | ||
$( document ).ready( function() { | ||
$( "a" ).on( "click", function customResponse( 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. It's cool to have |
||
alert( "As you can see, the link no longer took you to jquery.com." ); | ||
|
||
event.preventDefault(); | ||
} ); | ||
} ); | ||
``` | ||
|
||
Try replacing the first snippet of jQuery code, which you previously copied into `demo.js`, with the one above. Save `index.html` and reload the page to try it out. | ||
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. Replace
with
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. Shouldn't this say save |
||
|
||
### Alternative Setups | ||
|
||
You may encounter a somewhat similar strategy of running the code in an `onload` method 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. | ||
|
||
``` | ||
window.onload = function waitForIt() { | ||
alert( "This will be slower than using .ready() and is not recommended." ); | ||
}; | ||
``` | ||
|
||
In other cases you may want to alias the jQuery namespace to avoid potential conflicts with other JavaScript libraries that also use `$`. The `ready` method can take an argument, so you can pass it callback 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( $ ) { | ||
// 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.
There is a spacing problem here.