将参数传递给 javascript(不是函数)
我正在开发一个 javascript 小部件,我需要将参数发送到 Javascript,或者能够访问 HTML 的 SCRIPT 标记中定义的变量。例如,谷歌分析。下面的 GA 代码使 _setAccount 和 _trackPageview 等变量可用于 javascript,我需要执行相同的操作。我怎样才能做到这一点?调用的javascript如何https://ssl.google-analytics.com/ga.js 或 http://www.google-analytics.com/ga.js 在此case 访问定义的变量?
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
非常感谢任何帮助。
TIA, 詹姆斯.
I'm developing a javascript widget, and I need to send parameters to Javascript, or be able to access variables defined within SCRIPT tag of an HTML. For example, Google Analytics. The GA code below makes variables like _setAccount and _trackPageview available to the javascript, and I need to do the same. How can I achieve that? How can the called javascript https://ssl.google-analytics.com/ga.js or http://www.google-analytics.com/ga.js in this case access the defined variables?
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
Any help is highly appreciated.
TIA,
James.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过添加的
标记引入的 Google 代码只是期望能够访问名为“_gaq”的全局(
window
)变量,并且它期望它成为具有特定属性的数组等。The Google code that's pulled in via the added
<script>
tag simply expects to be able to access a global (window
) variable named "_gaq", and it expects it to be an array with particular properties, etc.