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 - Why is my script failing w3c validation? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my script failing w3c validation? - Stack Overflow

programmeradmin4浏览0评论

I have this piece of code:

<script language="javascript" type="text/jscript">
  document.write("<img src='.lgt?en=P.........TP_Q=&amp;ur=' + escape(document.referrer) + ''  border='0' alt='no alt' />");
</script>

and... when I try to validate it, I'm given this error:

document type does not allow element "img" here

…rer) + '" border="0" alt="no alt" />');

The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

Any idea what I can do to make this JavaScript w3c pliant?

I have this piece of code:

<script language="javascript" type="text/jscript">
  document.write("<img src='http://dm.leadgenesys./jpgp.lgt?en=P.........TP_Q=&amp;ur=' + escape(document.referrer) + ''  border='0' alt='no alt' />");
</script>

and... when I try to validate it, I'm given this error:

document type does not allow element "img" here

…rer) + '" border="0" alt="no alt" />');

The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

Any idea what I can do to make this JavaScript w3c pliant?

Share Improve this question edited Jun 4, 2009 at 22:12 Shog9 160k36 gold badges235 silver badges240 bronze badges asked Jun 4, 2009 at 22:03 AndyAndy 5251 gold badge7 silver badges18 bronze badges 1
  • The language attribute is deprecated and there's no text/jscript value for the type attribute. Well, there's not text/javascript either, but that's what browsers are used to see right there. – Ionuț G. Stan Commented Jun 4, 2009 at 22:17
Add a ment  | 

6 Answers 6

Reset to default 5

Simple: don't try to validate your JavaScript as HTML.

You can do this in a number of ways... But the best by far is to move it out into a separate JS file, and then either call it from a short script

<body>
...
<script type="text/javascript" language="javascript">WriteImage();</script>
...
</body>

...or better yet, ditch document.write() entirely and manipulate the DOM after it has loaded.

See also: Unobtrusive JavaScript

Another way to silence the validator...

Put it like this:

<script type="text/javascript">
/* <![CDATA[ */
your_javascript_here("<" + ... ;
/* ]]> */
</script> 

The CDATA part should be enough for the validator, the /* style ments */ are for older browsers which do not recognize the CDATA tag (it would otherwise break the javascript).

How aboout

<script>
<!--
var i = null;
if ((i/0) == 12)
  alert("whooo! pack your things, as it's starting to rain cats and dogs!");
--><script>

See that the content of the "script" is inside "html ments"?

You probably have your <script> element in your <head>, so essentially you're trying to create an <img> in the head of your document, which doesn't work. You need to put the script in the <body>.

However, I'd remend you add the element via the DOM instead, because you'll be less likely to run into these kinds of problems.

It's part of the HTML4 standard. You must escape "</" sequences found inside SCRIPT tags.

<SCRIPT type="text/javascript">
  document.write ("<EM>This will work<\/EM>")
</SCRIPT>

Another solution, and probably better, is to move the JS code in external files.

Reference

document.write("<" + "img … /" + ">");

Or:

document.write("\x3Cimg … /\x3E");
发布评论

评论列表(0)

  1. 暂无评论