在哪里放置JavaScript配置功能? [英] Where to put JavaScript configuration functions?

查看:75
本文介绍了在哪里放置JavaScript配置功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在文件中包含javascript代码而不是将其包含在脚本标记上的一般开发人员意见是什么。

What is the general developer opinion on including javascript code on the file instead of including it on the script tag.

所以我们都同意需要包含jquery使用脚本文件,如下所示:

So we all agree that jquery needs to be included with a script file, like below:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"
         type="text/javascript"></script>

我的问题是,为了获取不在网站所有页面上的页面上的功能。我们是否在同一页面或上面称为mysite.js的全局包含文件中包含如下所示的函数。

My question is, in order to get functions on a page that is not on all pages of a site. Do we include the functions like below in the same page or in a global include file like above called mysite.js.

$(document).ready(function(){
$(".clickme").click(function(event){
  alert("Thanks for visiting!");
 });
});

确定。所以问题是:如果上面的代码将在每个类中被调用=特定页面上的clickme,并且您可以从名为mysite.js的包含单独文件或在内容中调用它。页。你会选择哪种方式?

ok. So the question is: if the code above is going to be called in every class="clickme" on a specific pages, and you have the ability to call it either from an include separate file called mysite.js or in the content of the page. Which way will you go?

参数是:


  • 如果您将其包含在页面上,则只会从那些需要js功能的特定页面中调用它。

  • If you include it on the page you will only call it from those specific pages that the js functionality is needed.

或者你将它包含在一个浏览器缓存的文件中,但是jquery必须花费x ms才能知道该函数不会在没有clickme类的页面上触发。

Or you include it as a file, which the browser cached, but then jquery will have to spend x ms to know that that function is not trigger on a page without "clickme" class in it.

编辑1:
好​​的。我想确保人们解决的一点是,如果将document.ready函数称为页面中不存在的东西,会触发浏览器上的任何类型的延迟吗?这是一个重大影响吗?

EDIT 1: Ok. One point that I want to make sure people address is what is the effect of having the document.ready function called things that does not exist in the page, will that trigger any type of delay on the browser? Is that a significant impact?

推荐答案

首先 - $(#clickme)会找到id =clickme而不是class =clickme。如果你正在寻找课程,你想要 $(。clickme)

First of all - $("#clickme") will find the id="clickme" not class="clickme". You'd want $(".clickme") if you were looking for classes.

我(尝试)永远不要在我的XHTML文档中放入任何实际的JavaScript代码,除非我正在快速测试页面上的内容。我总是链接到外部JS文件来加载我想要的功能。没有JS的浏览器(如网页抓取工具)不会加载这些文件,它会让你的代码看起来更清晰查看来源。

I (try to) never put any actual JavaScript code inside my XHTML documents, unless I'm working on testing something on a page quickly. I always link to an external JS file to load the functionality I want. Browsers without JS (like web crawlers) will not load these files, and it makes your code look much cleaner to the "view source".

如果我需要一点点仅在一个页面上的功能 - 它有时会获得自己的包含文件。这一切都取决于它使用了多少功能/慢选择器。仅仅因为你将JS放在外部JS文件中并不意味着你需要将它包含在每一页上。

If I need a bit of functionality only on one page - it sometimes gets its own include file. It all depends on how much functionality / slow selectors it uses. Just because you put your JS in an external JS file doesn't mean you need to include it on every page.

我使用这种做法的主要原因 - 如果我需要的话要改变一些JavaScript代码,它们都会在同一个地方,并在网站范围内进行更改。

The main reason I use this practice - if I need to change some JavaScript code, it will all be in the same place, and change site wide.

就性能问题而言 - 一些选择器会占用很多时间,但大多数(特别是那些处理身份证的人)都很快。搜索不存在的选择器是浪费时间,但当你把它放在第二个脚本HTTP请求的浪费时间(阻止DOM准备好btw)时,搜索空选择器通常会赢作为两个邪恶中较小的一个。 jQuery 1.3 Performace Notes SlickSpeed 有望帮助您确定您在搜索课程时失去了多少MS。

As far as the question about performance goes- Some selectors take a lot of time, but most of them (especially those that deal with ID) are very quick. Searching for a selector that doesn't exist is a waste of time, but when you put that up against the wasted time of a second script HTTP request (which blocks the DOM from being ready btw), searching for an empty selector will generally win as being the lesser of the two evils. jQuery 1.3 Performace Notes and SlickSpeed will hopefully help you decide on how many MS you really are losing to searching for a class.

这篇关于在哪里放置JavaScript配置功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆