te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>html - Where is the best place to put JavaScriptAjax in a document? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Where is the best place to put JavaScriptAjax in a document? - Stack Overflow

programmeradmin3浏览0评论

Apologies for the dumb sounding question, but I need the experts to clarify.

Out of the three places to put JavaScript, head, $(document).ready, or body, where would the best place be to put some ajax that uses a lot of $GET functions?

For instance I am using a JavaScript function called execute_send() but I am unsure where the best place to put it would be. Below is the error:

Problem at line 67 character 22: 'execute_send' was used before it was defined.
function execute_send() {

Also how does the placement affect the page loading time?

Apologies for the dumb sounding question, but I need the experts to clarify.

Out of the three places to put JavaScript, head, $(document).ready, or body, where would the best place be to put some ajax that uses a lot of $GET functions?

For instance I am using a JavaScript function called execute_send() but I am unsure where the best place to put it would be. Below is the error:

Problem at line 67 character 22: 'execute_send' was used before it was defined.
function execute_send() {

Also how does the placement affect the page loading time?

Share Improve this question edited Feb 1, 2022 at 11:42 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 6, 2011 at 3:15 John DoeJohn Doe 3,68115 gold badges64 silver badges112 bronze badges 1
  • 3 $(document).ready is not a "place." It is JavaScript code (specifically, jQuery, a JS library) just like any other. – Matt Ball Commented Oct 6, 2011 at 3:17
Add a ment  | 

4 Answers 4

Reset to default 10

In general, unless for some reason you need it elsewhere, put all of your JS last in the body. The browser won't continue until it's parsed your JS, so it is nice to let the page load first. See http://developer.yahoo./performance/rules.html

As an example of when you might actually want to put JS in the head: You might have some A/B testing code that you want to run before the page even renders - in that case, the code should go in the head, because you really do want it to run as soon as possible.

As @Thom Blake said, in general the best place is at the bottom of the <body> (+1 for that) - but I'll expand on that a bit:

The reason for this is that as the browser loads the page, it has to stop loading and parse the JavaScript when it encounters it. So if you have all your scripts in the <head> for instance, there will be a delay in loading all the content in the <body>

Note that this is a delay in loading - separate from the actual execution of the script. Something like $(document).ready() deals with when the script is executed, not with when it is loaded.

Generally, all this matters because you want a web page to feel as responsive as possible, so a best practice list for JavaScript will usually be along these lines:

  1. Place all your scripts at the bottom of the <body> so that the loading of non-JS resources, such as images, is not delayed.

  2. Combine your scripts into a single file, so that the server has to make fewer requests for resources (you'll see this referred to as "minimizing HTTP requests")

  3. Minify you scripts, to reduce their total size, which speeds up loading times

  4. Put any code reliant on the DOM (eg click handlers, HTML manipulation, etc) inside $(document).ready() (or the equivalent method for the JS library in use on the page).

Same subject : whats-pros-and-cons-putting-javascript-in-head-and-putting-just-before-the-body

In the past, i experienced some jquery problems has it was not 'loader' when initialising .. this is why we decided to insert it in the <head>.

In some situations it's not easy to move scripts to the bottom. If, for example, the script uses document.write to insert part of the page's content, it can't be moved lower in the page. There might also be scoping issues. In many cases, there are ways to workaround these situations.

For the rest of javascripts, all before the closing </body> tag.

To explain the 'Why page will load faster' : It wont.

Browsers are single threaded, so it’s understandable that while a script is executing the browser is unable to start other downloads. But there’s no reason that while the script is downloading the browser can’t start downloading other resources. And that’s exactly what newer browsers, including Internet Explorer 8, Safari 4, and Chrome 2, have done.

The difference is visible has your ressources within the <body> tag will load/show sequencialy. When the browser gets to load <script src=...js> the plete file has to be loader before the browser can fetch another ressource. So, it's an illusion, because the browser will load/di more 'visible' content before 'javascripts'.

To visualise the whole thing : firebug > Net (tab)

As stated before, $(document).ready is not a place. (For jQuery, $(document).ready simply ensures that the DOM is fully loaded (ready to manipulate) before any script is executed.) You would place your JavaScript in the <head> or the <body>.

However, putting all of your JavaScript includes and JavaScripts at the bottom of the <body> section is best for loading performance. "Progressive Rendering" and "Parallel Downloading" are blocked for everything below the scripts. If your scripts are the last thing on the page, you're not blocking anything.

This article explains it in more depth.

Example:

<html>
      <head>
        <!-- MY HEAD CONTENT - LOAD ALL CSS -->
      </head>

      <body>

        <!-- MY HTML CODE -->

            <!-- START javascript -->
            <script type="text/javascript" src="/ajax/jquery/jquery-1.6.2.min.js"></script>
            <script type="text/javascript" src="/ajax/jquery/plugins/jquery.random_plugin.js"></script>
            <script type="text/javascript" src="/includes/some_other_scripts.js"></script>
            <script type="text/javascript" language="JavaScript">
            //<![CDATA[
                $(document).ready(function(){
                    // my jQuery/JavaScript code
                });
            //]]>
            </script><!-- END javascript -->
      </body>
</html>
发布评论

评论列表(0)

  1. 暂无评论