【问题标题】:jquery visual website optimizer codejquery 可视化网站优化器代码
【发布时间】:2014-05-28 14:05:08
【问题描述】:

我有一个来自视觉网站优化器的 javascript 代码,它将被放置在 HTML 标头上:

var _vwo_code=(function(){
var account_id=7237,
settings_tolerance=2000,
library_tolerance=1500,
use_existing_jquery=true,
// DO NOT EDIT BELOW THIS LINE
f=false,d=document;return{use_existing_jquery:function(){return use_existing_jquery;},library_tolerance:function(){return library_tolerance;},finish:function(){if(!f){f=true;var a=d.getElementById('_vis_opt_path_hides');if(a)a.parentNode.removeChild(a);}},finished:function(){return f;},load:function(a){var b=d.createElement('script');b.src=a;b.type='text/javascript';b.innerText;b.onerror=function(){_vwo_code.finish();};d.getElementsByTagName('head')[0].appendChild(b);},init:function(){settings_timer=setTimeout('_vwo_code.finish()',settings_tolerance);this.load('//dev.visualwebsiteoptimizer.com/j.php?a='+account_id+'&u='+encodeURIComponent(d.URL)+'&r='+Math.random());var a=d.createElement('style'),b='body{opacity:0 !important;filter:alpha(opacity=0) !important;background:none !important;}',h=d.getElementsByTagName('head')[0];a.setAttribute('id','_vis_opt_path_hides');a.setAttribute('type','text/css');if(a.styleSheet)a.styleSheet.cssText=b;else a.appendChild(d.createTextNode(b));h.appendChild(a);return settings_timer;}};}());_vwo_settings_timer=_vwo_code.init();

但我只想在这种情况下运行此代码:

$(document).ready(function(){
  if($('#selectedLang').val() == 'en_US')
  {
    //run the visual website optimizer code
  }
});

我试过了,但返回错误“_vwo_code is not defined”

$(document).ready(function(){
  if($('#selectedLang').val() == 'en_US')
  {
    var _vwo_code=(function(){
    var account_id=7237,
    settings_tolerance=2000,
    library_tolerance=1500,
    use_existing_jquery=true,
    // DO NOT EDIT BELOW THIS LINE
    f=false,d=document;return{use_existing_jquery:function(){return use_existing_jquery;},library_tolerance:function(){return library_tolerance;},finish:function(){if(!f){f=true;var a=d.getElementById('_vis_opt_path_hides');if(a)a.parentNode.removeChild(a);}},finished:function(){return f;},load:function(a){var b=d.createElement('script');b.src=a;b.type='text/javascript';b.innerText;b.onerror=function(){_vwo_code.finish();};d.getElementsByTagName('head')[0].appendChild(b);},init:function(){settings_timer=setTimeout('_vwo_code.finish()',settings_tolerance);this.load('//dev.visualwebsiteoptimizer.com/j.php?a='+account_id+'&u='+encodeURIComponent(d.URL)+'&r='+Math.random());var a=d.createElement('style'),b='body{opacity:0 !important;filter:alpha(opacity=0) !important;background:none !important;}',h=d.getElementsByTagName('head')[0];a.setAttribute('id','_vis_opt_path_hides');a.setAttribute('type','text/css');if(a.styleSheet)a.styleSheet.cssText=b;else a.appendChild(d.createTextNode(b));h.appendChild(a);return settings_timer;}};}());_vwo_settings_timer=_vwo_code.init();
  }
});

请帮助我解决这个问题?提前致谢!

【问题讨论】:

    标签: jquery web optimization


    【解决方案1】:

    VWO 需要的是让 _vwo_code 变量在全局范围内可用,但是当您在文档就绪处理程序中定义它时,它仍然在本地范围内,因此无法在外部访问。

    所以要解决这个问题,你可以写

    window._vwo_code=(function(){
    

    而不是

    var _vwo_code=(function(){
    

    但请记住,现在 VWO 代码在文档准备好后运行,您可能会在测试页面上看到闪烁(应用更改之前显示的原始内容)。

    (注:我在 VWO 工作)

    【讨论】:

    • 我刚刚注意到这个错误,我该如何解决这个问题? vwo_$.parseJSON is not a function
    • 您是否在变体更改中使用vwo_$.parseJSON
    • 它可能会迟到但可能会帮助某人。附加信息,如果您在打开网页时看到空白屏幕 - 检查 DOMContentLoaded 事件加载是否已经完成 - developer.mozilla.org/en-US/docs/Web/API/Document/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多