Combining code from multiple javascript blocks causes error?
We are using the following tracking code in our site for third party tracking. The code was copied from the third party site, and works fine, but notice that it is split into three javascript tag blocks. Since the blocks are right next to each other, I assumed I could combine the code into one block, and all would be well开发者_开发知识库 (I want to call this code in an AJAX callback section). However, when I combine the code, I get a "Uncaught ReferenceError: mm_variables not defined".
Given the code still executes in the same order and scope, I'm trying to understand how combining the tag sections would make any difference? Are there scoping issues I'm unaware of?
<!-- Begin Metrics Tracking Code -->
<script type="text/javascript">
var mm_c = 'd76e1f47616000000000000000';
var mm_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
document.write(unescape("%3Cscript src='" + mm_protocol + "www.mongoosemetrics.com/jsfiles/js-correlation/mm-getvar.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
/* Custom Parameters */
/* MANDATORY default_number Setup Parameter DO NOT REMOVE */
var default_number='8889996666'; /* 10 Digits Only i.e. 8881234567 */
</script>
<script type="text/javascript">
document.write(unescape("%3Cscript src='" + mm_protocol + "www.mongoosemetrics.com/jsfiles/js-correlation/mm-control.php%3F" + mm_variables + "' type='text/javascript'%3E%3C/script%3E"));
</script>
<!-- End Metrics Tracking Code -->
I assume that mm_variables
is defined in the script which is loaded through
document.write(unescape("%3Cscript src='" + mm_protocol + "www.mongoosemetrics.com/jsfiles/js-correlation/mm-getvar.js' type='text/javascript'%3E%3C/script%3E"));
In case you don't know, this creates a new script
element and adds it to the document. It will be something like:
<script src='http://www.mongoosemetrics.com/jsfiles/js-correlation/mm-getvar.js' type='text/javascript'></script>
You just cannot access variables defined in a script which is included dynamically in the same script
element.
The included script has to be loaded and executed first. It won't be executed until the current script
block finished.
You should be able to move var default_number='8889996666';
to the first block, but the last line must be in its own block.
Update: Regarding your comment. In this case you have to execute the last line inside the callback. You can change it from document.write
to DOM manipulation. The only problem is that this won't work if the included script itself further scripts via document.write
. You have to make sure that this is not the case. Then you could do:
var default_number='8889996666',
default_fooBar; // make a global empty variable
makeAjaxRequest(function(parameter) {
default_fooBar = paramter;
var script = document.createElement('script');
script.src = mm_protocol + "www.mongoosemetrics.com/jsfiles/js-correlation/mm-control.php%3F" + mm_variables;
document.getElementsByTagName('head')[0].appendChild(script);
});
Also keep in mind that this is a tracking script. It might not work properly if it is append after the DOM loaded (for whatever reason). Maybe they also provided some way to load parameters via Ajax (should be mentioned in their documentation).
精彩评论