我想將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