为了减少负载阻塞,我决定转换以下google自定义AdSense搜索代码...
<form action="http://www.google.ru" id="cse-search-box" class="form-search">
<div>
<input type="hidden" name="cx" value="partner-pub-7920375793574512:1188291711" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" name="q" size="55" />
<input type="submit" name="sa" value="Найти" class="btn btn-info" />
</div>
</form>
<script type="text/javascript" async src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script>
<script type="text/javascript" async src="http://www.google.com/cse/t13n?form=cse-search-box&t13n_langs=en"></script>
<script type="text/javascript" async src="http://www.google.ru/coop/cse/brand?form=cse-search-box&lang=ru"></script>
... to(我刚刚删除了脚本到require js
加载样式)
<form action="http://www.google.ru" id="cse-search-box" class="form-search">
<div>
<input type="hidden" name="cx" value="partner-pub-7920375793574512:1188291711" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" name="q" size="55" />
<input type="submit" name="sa" value="Найти" class="btn btn-info" />
</div>
</form>
...并且需要js模块:
var scripts;
scripts = ['http://www.google.com/jsapi', 'http://www.google.com/cse/t13n?form=cse-search-box&t13n_langs=en', 'http://www.google.ru/coop/cse/brand?form=cse-search-box&lang=ru'];
define(scripts, function() {
return google.load("elements", "1", {
packages: "transliteration"
});
});
得到了不可预知的结果:
当页面首先加载时它没问题,但后来我看到没有任何html元素的白色屏幕,谷歌Chrome控制台没有错误。
因此,在require js
模块中加载google脚本会破坏所有html。为什么会这样?
答案 0 :(得分:0)
我在行中添加了window.
return google.load("elements", "1", {packages: "transliteration"});
所以得到了一句话:
return window.google.load("elements", "1", {packages: "transliteration"});
一切正常。
require.js
模块的完整代码:
var scripts;
scripts = ['http://www.google.com/jsapi', 'http://www.google.com/cse/t13n?form=cse-search-box&t13n_langs=en', 'http://www.google.ru/coop/cse/brand?form=cse-search-box&lang=ru'];
define(scripts, function() {
return window.google.load("elements", "1", {
packages: "transliteration"
});
});