我想将JavaScript变量推送到数组中。具体而言,我想将Google Analytics帐号ID(UA-XXXXXXXX-X)推入我的网站页脚中的Google Analytics JavaScript跟踪代码。无法将javascript变量推入数组
(function($) {
var jsvars = {"columns":"1","mobilemenu":"1","googleanalytics":"UA-XXXXXXXX-X"};
var googleanalytics = jsvars.googleanalytics;
/* hit run to to confirm the googleanalytics variable is available */
alert (googleanalytics);
var _gaq = _gaq || [];
/* I want to push the googleanalytics variable into the array */
/* When I view source, it does not show as UA-XXXXXXXX-X, it just shows as googleanalytics */
_gaq.push(['_setAccount', googleanalytics]);
_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);
})();
})(jQuery);
http://jsfiddle.net/robflate/2MnM7/
感谢。
..但它的工作原理? – c69 2012-01-28 15:45:35
当您执行“查看源代码”时,您在JavaScript中执行的操作不会显示出来。这只是告诉你当浏览器加载它时页面看起来像什么。 – Pointy 2012-01-28 15:52:07
请在此处发布您的代码,而不是仅在其他网站上发布。如果这个链接永远消失了,你的问题将不会对未来的读者有用。 – 2012-01-28 15:54:04