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: Adding attribute dynamically to the <html > tag - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: Adding attribute dynamically to the <html > tag - Stack Overflow

programmeradmin5浏览0评论

I'm trying to add FB xmlns attribute to the document's <html> tag (<html lang="en" xml:lang="en" xmlns="">) dynamically. For some reason adding it like below does not work:

htmltag  = document.getElementsByTagName ('html');
htmltag[0].setAttribute("xmlns:fb","");");

How can I do it?

Thanks!

Update: No jquery or other lib is available.

I'm trying to add FB xmlns attribute to the document's <html> tag (<html lang="en" xml:lang="en" xmlns="http://www.w3/1999/xhtml">) dynamically. For some reason adding it like below does not work:

htmltag  = document.getElementsByTagName ('html');
htmltag[0].setAttribute("xmlns:fb","http://www.facebook./2008/fbml");");

How can I do it?

Thanks!

Update: No jquery or other lib is available.

Share Improve this question edited Aug 11, 2009 at 19:07 Nir asked Aug 11, 2009 at 18:57 NirNir 25.4k26 gold badges84 silver badges119 bronze badges 1
  • 2 Who should read that attribute? XML parsers don’t interpret JavaScript. – Gumbo Commented Aug 11, 2009 at 19:00
Add a ment  | 

4 Answers 4

Reset to default 5

Heh actually, after doing the jquery solution I stated above;

I realized it could be done easier with a single javascript line:

document.documentElement.setAttribute("xmlns:fb", "http://www.facebook./2008/fbml");

Keep in mind ... If you view the source with the browser, you will not see the attribute attached, as it programatically adds it after the page loads. Use Firebug in firefox, or something that lets you see the source being manipulated on the fly, and you should be good to go.

This probably isn't the answer you're wanting :)

Tacking on semantic markup with client-side code is not good practice. It hides the valuable machine-readable info from most machines. The HTML is already fetched, parsed and displayed by the time Javascript executes*. A dollar short and a day late!

If you can add the attributes on the server, before it is sent to the browser, go for it.

If you have to do it client-side; don't be tempted by a framework like jQuery or whatever. That's a huge overhead for a small task.

*non-scientific tests

This can be done very quickly with jquery.

Include:

(or point to a local jquery library)

and here's the code:

<script type="text/javascript">
$(document).ready(function() {
    $("html").attr("xmlns:fb", "http://www.facebook./2008/fbml");
});  </script>

To get reference to the <html /> element use

document.documentElement
发布评论

评论列表(0)

  1. 暂无评论