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

html - Why does my javascript only fire when the code is repeated? - Stack Overflow

programmeradmin3浏览0评论

I'm having an issue where I can only get my code to fire if I repeat it. See the below example. While the code is nearly identical if I take either of the two scripts out the code doesn't function with just one, yet if I run both the script fires fine, but only one instead of two. give it a go.

This works (but only one script fires):

<script type="text/javascript">
var url = window.location.href + "";
document.write('<script type="application/javascript" src=";mik21=' + url + '"/>');
</script>

<script type="text/javascript">
var url = window.location.href + "";
document.write('<script type="application/javascript" src=";mik=' + url + '"/>');
</script>

This does not work:

<script type="text/javascript">
var url = window.location.href + "";
document.write('<script type="application/javascript" src=";mik21=' + url + '"/>');
</script>

Or this:

<script type="text/javascript">
var url = window.location.href + "";
document.write('<script type="application/javascript" src=";mik=' + url + '"/>');
</script>

Does anyone have any ideas? this is definitely the weirdest thing I've seen in a while.

Thanks,

I'm having an issue where I can only get my code to fire if I repeat it. See the below example. While the code is nearly identical if I take either of the two scripts out the code doesn't function with just one, yet if I run both the script fires fine, but only one instead of two. give it a go.

This works (but only one script fires):

<script type="text/javascript">
var url = window.location.href + "";
document.write('<script type="application/javascript" src="https://testdomain./mik21?add=2140535&mik21=' + url + '"/>');
</script>

<script type="text/javascript">
var url = window.location.href + "";
document.write('<script type="application/javascript" src="https://testdomain./mik?add=2140535&mik=' + url + '"/>');
</script>

This does not work:

<script type="text/javascript">
var url = window.location.href + "";
document.write('<script type="application/javascript" src="https://testdomain./mik21?add=2140535&mik21=' + url + '"/>');
</script>

Or this:

<script type="text/javascript">
var url = window.location.href + "";
document.write('<script type="application/javascript" src="https://testdomain./mik?add=2140535&mik=' + url + '"/>');
</script>

Does anyone have any ideas? this is definitely the weirdest thing I've seen in a while.

Thanks,

Share Improve this question edited Jul 3, 2015 at 18:30 Salman Arshad 272k84 gold badges442 silver badges534 bronze badges asked Jul 3, 2015 at 9:20 Michael WaMichael Wa 1114 bronze badges 15
  • 2 don't use document.write is my first hint, who knows why that's even still valid in browsers since about 2005 – Jaromanda X Commented Jul 3, 2015 at 9:24
  • stackoverflow./questions/802854/… – birdspider Commented Jul 3, 2015 at 9:24
  • 2 is there something like <script src="..." /> – Arun P Johny Commented Jul 3, 2015 at 9:25
  • 3 Try it with <script …></script>. script requires a closing tag. – Sebastian Simon Commented Jul 3, 2015 at 9:31
  • 1 the code you shared works.. check in developer tools – salahuddin Commented Jul 3, 2015 at 9:38
 |  Show 10 more ments

3 Answers 3

Reset to default 11

The problem is that your script tag is broken. A <script> tag must be closed by </script>.
Now here is what happens:

Example 1

<script>
  document.write('<script src="test.js"/>');
</script>

Produces the following markup HTML:

<script>
  document.write('<script src="test.js"/>');
</script>
<script src="test.js">
  </body>
  </html>

In general, browsers do not execute a <script> block until they find the corresponding </script>. There is no explicit closing tag in the above example so the browser ignores the tag.

Example 2

<script>
  document.write('<script src="test.js"/>');
</script>
This HTML will be consumed
<script>
  document.write('<script src="test.js"/>');
</script>

Produces the following output:

<script>
  document.write('<script src="test.js"/>');
</script>
<script src="test.js">
  This HTML will be consumed
  <script>
  document.write('<script src="test.js"/>');
</script>

Notice that the dynamically written script tag is not closed. The browser will match this tag with the second </script> in the markup; everything in-between (incorrectly) bees part of this tag.

Solution

When using document.write, make sure that you close <script> tags properly. But note that you cannot use </script> as-is inside JavaScript code since it signals end of script block. You can use the following trick:

<script>
  document.write('<script src="test.js"><\/script>');
</script>

I have just tested this:

Consider the source

data:application/javascript;base64,Y29uc29sZS5sb2coIlRlc3QiKTs=

This points to the following code:

console.log("Test");

This snippet below generates

<script src="data:application/javascript;base64,Y29uc29sZS5sb2coIlRlc3QiKTs="/>

<div>A</div>
<script>
document.write('<script src="data:application/javascript;base64,Y29uc29sZS5sb2coIlRlc3QiKTs="/>');
</script>
<div>B</div>


However, this snippet below generates

<script src="data:application/javascript;base64,Y29uc29sZS5sb2coIlRlc3QiKTs="></script>

<div>A</div>
<script>
document.write('<script src="data:application/javascript;base64,Y29uc29sZS5sb2coIlRlc3QiKTs="></'+'script>');
</script>
<div>B</div>


The first snippet logs "Test" but doesn’t display “B” in the HTML. The second works properly. In the Real-time HTML Editor not even "Test" gets logged for the first one. The implication of that is that all the HTML below the <script/> (which is inserted as <script>) gets consumed as Salman A’s answer points out.

This is because the only valid syntax for <script> tags is <script></script> and not <script/>.

This works:

<script type="text/javascript">
var url = window.location.href + "";
document.write('<script type="application/javascript" src="https://testdomain./mik21?add=2140535&mik21=' + url + '"></scr'+'ipt>');
document.close();
</script>

To prevent js from crashing, i've split the <script> tag into two strings.

发布评论

评论列表(0)

  1. 暂无评论