-
Notifications
You must be signed in to change notification settings - Fork 482
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
Splitting intro into two tutorials #714
Open
connorcartwright
wants to merge
25
commits into
jquery:main
Choose a base branch
from
connorcartwright:658-split-jquery-setup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
aedb19a
Updated order file ready for new files, removed the old 'how-jquery-w…
5ec5a7e
Added the new setup and using jQuery files
b568186
Fix spacing issue
71f81e9
Set language to 'en' to improve accessibility
317a6a2
Added https version of link
658f1a2
Suggested using CDN as an alternative
8dd680a
Updated wording of calling the ready method
0731a06
Removed unnecessary named function
b756340
Corrected saving of the file and made link https
381f62f
Made link https
a3ae666
Updated wording
33dc80c
Corrected wording
cdb84df
https url
70a5aed
Corrected link to css
92711ce
Moved CSS to head
56db1b1
Corrected description
e0006aa
Made all links https
8634d09
Made remaining http links https
da532c7
Corrected reference to web inspector
ca1cd09
Updated wording of 'Right' to 'Correct'
e2f2d10
Changed ordering so the note is relevant and briefly clarified what t…
91c86fe
Updated wording as the user may have used a CDN
b42014c
Added code ticks around 'link'
f5e88e8
Updated wording (removed repetition of alter)
08844e0
Updated to use spaces
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<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 lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Demo</title> | ||
</head> | ||
<body> | ||
<a href="https://jquery.com/">jQuery</a> | ||
<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](https://jquery.com/download/) page and store the `jquery.js` file in the same directory as `index.html`. | ||
|
||
*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.* | ||
|
||
Alternatively you could use a [Content Delivery Network](https://code.jquery.com/) (CDN) where the file is hosted for you. | ||
|
||
After setting up the jQuery file you will need to add another JavaScript file, `demo.js`, in the same directory as `index.html`. The `src` attribute in the second `<script>` element must point to the location of `demo.js`, 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, you can use the jQuery [ready method](https://api.jquery.com/ready/): | ||
|
||
``` | ||
$( 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() { | ||
alert( "Thanks for visiting!" ); | ||
} ); | ||
} ); | ||
``` | ||
|
||
Copy the above jQuery code into `demo.js`, save it, and reload `index.html`. Clicking the link should now first display an alert pop-up, and then continue with the browser's default behavior of navigating to https://jquery.com. | ||
|
||
For `click` and most other [events](https://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 the first snippet of code, which you previously copied into `demo.js`, with the one above. Save the `demo.js` file and reload `index.html` to try it out. | ||
|
||
### 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. | ||
} ); | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It's cool to have
event
in this example because we use it.