I'm done with PC RegEx and moving on to jQuery. ;)
Knowing almost nothing about JavaScript and jQuery, a friend talked me into replacing all my old scripts with it. From what I've seen/read so far, it looks pretty easy.
First, my preference is to load all JS just before the </body>
tag. Is that okay with jQuery or should it be loaded in the <head>
? Can't find any discussion on this.
What about the "child" jQuery scripts? I assume that because of the $(document).ready(function()
, I am able to load them anywhere I want. Right?
I'll be replacing a few old scripts I've been using:
Does jQuery do fra开发者_StackOverflow社区me busting? Thinking not.
Does jQuery do form/element focusing? Thinking yes.
EDIT:
Sorry about all the extra questions.
What I meant by "child scripts" is "all of my normal scripts," such as:
<script type="text/javascript" src="tooltips.js"></script>
<script type="text/javascript" src="popups.js"></script>
Here's my rundown of where to get started with jQuery -
Download the HTML documentation and have a sift through to get a feel for how the library is structured and what commands are at your disposal. Having the documentation locally is faster and the search functionality works really well.
Take a look at the notes on the jQuery event object as they explain some fundamental concepts for how the event model works.
Once familiar with the core API, you may find that you need to write plugins for bespoke functionality required in your pages. The plugin authoring guide is a must read at this point as it will encourage good habits in code structure.
Subscribe to jQuery blogs. Not only will this keep you up to date with possible new features in upcoming releases, but will also provide insight into areas of the framework that you are not familiar with. Some I can recommend are (in no particular order):
- Brandon Aaron
- Dean Edwards
- James Padolsey
- John Resig
- jQuery Blog
- jQuery for Designers
- jQuery Minute
- Learning jQuery
- Paul Irish
- Remy Sharp
- Cody Lindley
To answer your specific questions,
$(document).ready(function() { ... })
(also shorthand $(function() { ... });
) will execute as soon as the DOM has loaded. This is usually where you want to put set up code for the page.
Having other scripts in the page is fine too. If you're more comfortable putting the script references at the bottom of the page then go for it (although after the first visit to your site, scripts are usually cached). If you can have certain scripts served from a CDN, such as the jQuery script from google API, then use this as chances are that the script will already be cached for the client.
I've not seen anything in jQuery core for framebusting, it's either something you'll need to implement yourself or have a look at the myriad of plugins already written - Chances are, if it's a common scenario and useful, someone's already written it.
When you say form element focusing, what do you mean? Do you mean if a certain condition is satisfied whilst an element has focus, that the focus jumps to another/the next element? Again, I've not seen it in the jQuery core, but take a look at the plugins, I imagine it's already been done. Or, this could be your first jQuery plugin project :)
You should really split up the unique questions into individual questions on here. But overall the main thing to remember is that jQuery is simply a framework on top of Javascript. It just makes things easier to do and (mostly) consistent across browsers.
To answer your first question, putting all your scripts at the end of the <body>
is fine. If you wrap everything in $(document).ready(function(){});
calls, it won't matter because it waits till the browser is ready for Javascript to start acting on the DOM.
Other than that, simply read through http://docs.jquery.com
From alot of the examples I've seen of jQuery, placing it at the bottom of the page is the new trend.
Once you have $(document).ready
you should be able to keep placing 'child scripts' in between the open braces.
I have no idea abou the frame busting, but if you can do it with Javascript, nothing is stopping you from using that same script in jquery.
You should be to do form/element focusing just like you would with javascript itself.
Have fun with jQuery and hope this helps some.
the general syntax for jquery is:
$(document).ready(function(){
[insert all your jquery scripts to run at load here]
});
this should go in the head section of the page and will then run when the document is ready to run dom
Well, it's generally a good idea to put all of the scripts you can in the tags. This isn't totally necessary, but it's done because A: It makes them easier to find, and B: There are some browsers, or at least there used to be, that would actually render your javascript to the screen if scripting was turned of.
I'm not sure what you mean by "child" scripts but functions declared "normally" outside the document ready block will work anywhere. Functions declared inside are going to be available inside that block only. Take a look at this:
function bar() {
alert('test');
}
$(document).ready(function() {
var foo = function() {
alert('do something!');
};
foo(); //works
bar(); //works
});
bar(); //works
foo(); //error
jQuery does to element focusing like so:
<input type="text" id="myInput"/> <button onclick="$('#myInput').focus();">Focus</button>
as far as Framebusting goes, whatever script you're currently using to do that should work alongside your jQuery, but there's not really built-in jQuery frame busting logic.
Scripts go at the bottom of the document for speed reasons. While your browser is downloading javascript, it isn't doing anything else (including downloading the rest of the document).
You can load child Javascript files anywhere, but again, bottom of the page is best. A typical setup is:
- Reference jQuery (preferably the one hosted at Google: http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js - doing so highly increases the likelihood that the user already has a cached version in their browser, which means they won't need to download it again
- Reference child objects
- Call $(document).ready
For further information on where to put you Javascript files, use the ySlow plugin and read everything by Steve Souders ("Even Faster Websites" comes to mind, although it's a bit high level).
Frame busting should be easy enough to do on your own, and jQuery comes with some cool selectors and enhanced functionality for forms.
精彩评论