3

我的网站上有几个嵌入代码,例如:

嵌入代码#1:

<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/f8Lp2ssd5A9ErAc&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/f8Lp2A9ErAc&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>

嵌入代码#2:

<script type="text/javascript">
_qoptions={
qacct:"p-3asdb5E0g6"
};
</script>
<script type="text/javascript" src="http://edge.quantserve.com/quant.js"></script>
<noscript>
<a href="http://www.quantcast.com/p-3asdb5E0g6" target="_blank"><img src="http://pixel.quantserve.com/pixel/p-3asdb5E0g6.gif" style="display: none;" border="0" height="1" width="1" alt="Quantcast"/></a>
</noscript>

等等..

如何组织它们并将它们分成一个外部的单个 js 文件以保持标记干净?

谢谢你的帮助!

4

1 回答 1

2

我认为 jQuery 没有太大帮助,我要么直接 document.write 它们,要么将它们存储在 xml 文件中

使用 document.write 它会是这样的(noscript 被删除,因为在 js 文件中没有任何意义

// --- starts jsfile
var embeds = [
'<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/f8Lp2ssd5A9ErAc&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/f8Lp2A9ErAc&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>',

'<script type="text/javascript">_qoptions={qacct:"p-3asdb5E0g6"};</script><script type="text/javascript" src="http://edge.quantserve.com/quant.js"></script>'
]; // notice the lack of comma on the last embed
function putEmbed(idx) {
  document.write(embeds[idx]);
}
// ------ end ------

然后使用

<script type="text/javascript">
putEmbed(0); // youtube
</script>

然后

<script type="text/javascript">
putEmbed(1); // quant
</script>
于 2010-04-26T18:11:06.927 回答