script loading error
Given piece of code works on firefox but not in Internet explorer. In IE, it says that COMSCORE is not defined.
document.writeln('<!-- Begin comScore Tag -->');
document.writeln('<sc'+'ript src="http://someURL/beacon.js">');
document.writeln('</sc'+'ript>');
document.writeln('<sc'+'ript>');
document.writeln(' COMSCORE.beacon({');
document.writeln(' c1:2,');
document.writeln(' c2:7290414,');
document.writeln(' c3:"",');
document.writeln(' c4:"",');
document.writeln(' c5:"",');
document.writeln(' c6:"",');
document.writeln(' c15:""');
document.writeln(' });');
document.writeln('</sc'+'ript>');
But below code works in both the browsers.
<script>
document.write(unescape("%3Cscript src='http://someURL/beacon.js' %3E%3C/script%3E"));
</script>
<script>开发者_如何学C;
COMSCORE.beacon({
c1:2,
c2:7290414,
c3:"",
c4:"",
c5:"",
c6:"",
c15:""
});
</script>
Basically in both the cases I am tring to load an external javascript file and trying to use a variable defined in that. I understand that in first case error might be due to asynchronous loading of JS file. But
1) Why is it working fine in firefox. Is there any difference in JS file loading mechanism in both the browsers
2) Why it worked in IE in second case. The code tries to work in the same way as in first case.
Try this if you must:
var comScoreText = '<!'+'-- Begin comScore Tag -->'; // not really interesting to write this comment, but if you do, escape it!
comScoreText += '<script src="http://someURL/beacon.js"><\/script>'; // you only need to escape the end tag's slash
document.write(comScoreText); // will load the script now
document.write('<script>if (COMSCORE) COMSCORE.beacon({ c1:2,c2:7290414,c3:"",c4:"",c5:"",c6:"",c15:""})<\/script>');
or if you need a var:
document.write('<script>if (COMSCORE){ var var1= 7290414; COMSCORE.beacon({ c1:2,c2:var1,c3:"",c4:"",c5:"",c6:"",c15:""})}<\/script>');
or
var var1 = 7290414
document.write('<script>if (COMSCORE) COMSCORE.beacon({ c1:2,c2:'+var1+',c3:"",c4:"",c5:"",c6:"",c15:""})<\/script>');
精彩评论