如何将内联JavaScripts合并为一个? [英] How to combine inline JavaScripts into one?

查看:88
本文介绍了如何将内联JavaScripts合并为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修复我们在我们的某个网站上使用的模板,其中包含以下代码

I'm fixing up a template we're using on one of our sites which has the following code

此代码段有效。

<script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js"></script>

<script type="text/javascript">
  GS_googleAddAdSenseService("ca-pub-123");
  GS_googleEnableAllServices();
</script>

<script type="text/javascript">
  GA_googleAddSlot("ca-pub-123", "Foo");
  GA_googleAddSlot("ca-pub-123", "Bar");
</script>

<script type="text/javascript">
  GA_googleFetchAds();
</script>

我试过连接像这样的静态脚本

I've tried concatenating the static scripts like this

<script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js"></script>

<script type="text/javascript">
  GS_googleAddAdSenseService("ca-pub-123");
  GS_googleEnableAllServices();

  GA_googleAddSlot("ca-pub-123", "Foo");
  GA_googleAddSlot("ca-pub-123", "Bar");

  GA_googleFetchAds();
</script>

但是,现在我收到错误

Uncaught ReferenceError: GA_googleAddSlot is not defined 

我'当谈到JavaScript的东西时,我没有菜鸟,但我无法想象为什么将3个内联脚本组合成一个< script> 标签会在这里产生任何不同。

I'm no noob when it comes to JavaScript stuff, but I can't imagine why combining the 3 inline scripts into a single <script> tag would make any difference here.

任何想法?

推荐答案

google_service。 js 未定义 GA_googleAdSlot ,但它定义了 GS_googleEnableAllServices 。当调用 GS_googleEnableAllServices 时,它使用 document.write 插入一个新的脚本元素,该元素加载<$ c的定义$ C> GA_googleAdSlot 。在当前正在执行的脚本元素结束之后,新的脚本元素将插入到文档中。这很复杂,但这是你的答案。

google_service.js does not define GA_googleAdSlot, but it defines GS_googleEnableAllServices. When GS_googleEnableAllServices is called, it uses document.write to insert a new script element which loads a definition of GA_googleAdSlot. The new script element is inserted in the document after the end of the script element currently being executed. It's complicated, but it's your answer.

这篇关于如何将内联JavaScripts合并为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆