google.load 问题
嗨,我在 momemt 上摆弄 google ajax api,并按照文档中的示例进行操作,我的 html 文件中有两个脚本标记:
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script language="Javascript" type="text/javascript">google.load('search', '1');</script>
一切正常,但当我使用 jquery 并尝试调用 google.js 时,它不会工作。加载('搜索','1');在 $(document).ready(function() 之后的外部 javascript 文件中,
我收到以下错误: null is null or not an object。
我显然错过了一些基本的东西,因为我刚刚学习 javascript,但我的印象是它最好不引人注目地使用 javascript。实际上包含一些 js 代码的第二个脚本标记不是不引人注目的。有人可以提供任何帮助吗?
Hi I am messing around with google ajax api at the momemt and following the examples from the documentation I have two script tags in my html file:
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script language="Javascript" type="text/javascript">google.load('search', '1');</script>
All works fine, but it will not work when I am using jquery and trying to call the google.load('search', '1'); in an external javascript file after $(document).ready(function()
I get the following error: null is null or not an object.
I am obviously missing something fundamental as I am just learning javascript but I was under the impression that it is best to use javascript unobtrusively. The second script tag that actually contains some js code isnt unobtrusive. Can anyone lend any help with this please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的解释,您的页面似乎设置如下:
但是,此不会按您预期的方式工作,因为
document.ready
事件在 DOM 之前不会触发已完全加载。然而,JavaScript 文件是在加载时执行的。因此,实际执行如下所示:根据其余代码的外观,您可能希望将所有初始化代码放在单独的文件中,或者移动您的 < code>search 加载回主文档。
关于不显眼的代码:
David,不显眼的 JavaScript 与它如何影响页面有关,而不是与它是在页内还是外部有关。
更重要的是不要让您的网站如此依赖 JavaScript,以至于在禁用 JavaScript 的情况下它无法运行。
例如,这是很唐突的:
因为它只能在启用 JavaScript 的情况下工作。此外,代码是内联的,这很糟糕,因为它没有将功能与结构(HTML)分开。
然而,采用一段类似的代码:
并使用此 javascript/jquery 片段:
Is 变得不引人注目,因为页面仍然可以工作(默认情况下加载 /do/something),但当启用 JavaScript 时,它会以更好的方式工作(执行 javascript而不是加载该网址)。这也称为渐进增强。
From what you have explained it seems your page is setup something like this:
However, this will not work as you expect since the
document.ready
event will not fire until the DOM is completely loaded. JavaScript files, however, are executed as they are loaded. So the actual execution looks like this:Depending on what the rest of your code looks like, you might want to either put all your initialization code in a separate file, or move your
search
load back into the main document.ABOUT UNOBTRUSIVE CODE:
David, unobtrusive JavaScript has to do with how it affects the page, not with whether or not it is in-page or external.
It is more about not making your site so dependent on JavaScript that it does not function with it disabled
For instance, this is obtrusive:
Because it will only work with JavaScript enabled. Additionally the code is inline which is bad because it does not separate functionality from structure (HTML).
However, taking a similar piece of code:
and using this javascript/jquery snippet:
Is becomes unobtrusive because the page still works (loads /do/something by default), but it works in a nicer way when JavaScript is enabled (executes the javascript instead of loading that url). This is also called Progressive Enhancement.