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; } ?>javascript - Place 'script' before the end of BODY tags with jQuery - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Place 'script' before the end of BODY tags with jQuery - Stack Overflow

programmeradmin3浏览0评论

This is the 'script' I want before the 'body' tag:

<script type="text/javascript">
  var vglnk = { api_url: '//api.viglink/api',
                key: '89dcd0a12ff35d227eaaaff82503030b' };

  (function(d, t) {
    var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;
    s.src = ('https:' == document.location.protocol ? vglnk.api_url :
             '//cdn.viglink/api') + '/vglnk.js';
    var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r);
  }(document, 'script'));
</script>

I want this code to be where I've put "HERE"

<html>
<head>
</head>

<body>
    Some HTML and stuff
    HERE
</body>

</html>

How would I go about this in jQuery? (I'm doing this from an extension. Mainly in Chrome, but also Firefox and Internet Explorer.)

This is the 'script' I want before the 'body' tag:

<script type="text/javascript">
  var vglnk = { api_url: '//api.viglink./api',
                key: '89dcd0a12ff35d227eaaaff82503030b' };

  (function(d, t) {
    var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;
    s.src = ('https:' == document.location.protocol ? vglnk.api_url :
             '//cdn.viglink./api') + '/vglnk.js';
    var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r);
  }(document, 'script'));
</script>

I want this code to be where I've put "HERE"

<html>
<head>
</head>

<body>
    Some HTML and stuff
    HERE
</body>

</html>

How would I go about this in jQuery? (I'm doing this from an extension. Mainly in Chrome, but also Firefox and Internet Explorer.)

Share Improve this question edited Nov 22, 2021 at 3:16 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Mar 25, 2013 at 22:45 Kyle Sterling CollinsKyle Sterling Collins 791 gold badge2 silver badges4 bronze badges 4
  • 1 stackoverflow./a/5114084/139010 – Matt Ball Commented Mar 25, 2013 at 22:47
  • 1 How would you suggest that I go about using this script on all pages? – Kyle Sterling Collins Commented Mar 25, 2013 at 22:55
  • 1 After a test, doesn't work. Won't allow the JS to do what it needs to do on a live page. – Kyle Sterling Collins Commented Mar 25, 2013 at 23:14
  • Related: Where should I put <script> tags in HTML markup? – Peter Mortensen Commented Nov 21, 2021 at 17:04
Add a ment  | 

2 Answers 2

Reset to default 11

You need the content script to do the insert on every page you want.

The code of the content script is really simple and doesn't need jQuery.

var code = "your script code here";
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.appendChild(document.createTextNode(code));
document.body.appendChild(script);

As it will only be called once you don't even need to define a function. You can debug the code using debugger on any web the content script is attaching (F12) you will see your code in the content script tab.

I had the same issue regarding the best place to add jQuery: to the header or before the body tag? The answer is that it does not matter.

The whole page (or DOM) needs to initialize or load in order to acplish what you are doing.

And...

The more information within the body, the more reliance you need to make sure the document is loaded.

The two sentences above are redundant because:

All jQuery UI, basic syntax, widgets, etc. are triggered with:

$(document).ready( function() {
    $("#some_id").click( function {
        More code here
    });
});`

The code above means that the the full HTML page (or 'document') needs to be loaded before jQuery can run, AKA initialized.

There is an order that jQuery needs to be loaded for a UI to work. The actual library needs to be first, then the UI. The library can be downloaded from jquery. and uploaded to the designers web space or through a CDN (content display network) to save on bandwidth. Here is an example of how the order should be:

<script src="http://code.jquery./jquery-1.6.4.min.js"></script>
<script src="http://code.jquery./mobile/1.0/jquery.mobile-1.0.min.js"></script>

Notice the library is the first line, and then the UI. In this case I loaded jQuery Mobile.

In conclusion, it does not matter. It is a preference mostly. More in on Unclear where $(document).ready goes.

发布评论

评论列表(0)

  1. 暂无评论