2011-10-17 92 views
0

我试图在jquery ajax page中添加google plus按钮,我遇到了一些麻烦。jquery ajax成功加载js

我的代码导致Uncaught TypeError: Cannot read property 'prototype' of undefined和loadind js失败到一个ajax页面。那么如何解决?谢谢。

$.ajax({ 
    url: url, 
    dataType: "html", 
    type: 'POST', 
    data: data, 
    success: function(html){ 
    $('#result').html(html); 
    $('#googleplus').html('<g:plusone href="'+window.parent.document.URL+'" size="tall"></g:plusone>'); 
    $.getScript("https://apis.google.com/js/plusone.js"); 
    } 
}); 
+0

而哪一行导致该错误? – JJJ

+0

@Juhana,不是在我的代码,但在'googleapis.client__plusone.js:82'然后失败谷歌加按钮。 –

回答

0

您可能需要使用$ .getscript的回调函数在脚本加载后插入按钮。

编辑我的答案,思考相同的原产地政策,但json应该可以跨域与getscript。

不知道,如果它的工作原理,但尝试:

$.getScript("https://apis.google.com/js/plusone.js", function() { 
    $('#googleplus').html('<g:plusone href="'+window.parent.document.URL+'" size="tall"></g:plusone>'); 
}); 

编辑:其实刚才测试了这个自己,这工作得很好,+1我。

再次编辑,如果它仍然不起作用,你的一个变量是错误的,或者你试图将按钮插入到一个不存在的div中。 为了确保股利存在,如果它在HTML阿贾克斯用你,你可以调用的完整的处理功能,例如:

$.ajax({ 
    url: url, 
    dataType: "html", 
    type: 'POST', 
    data: data, 
    success: function(html){ 
     $('#result').html(html); 
    }, 
    complete: function() { 
     $.getScript("https://apis.google.com/js/plusone.js", function() { 
      $('#googleplus').html('<g:plusone href="'+window.parent.document.URL+'" size="tall"></g:plusone>'); 
     }); 
    } 

}); 

之后,你应该检查你的增值经销商,看到窗口.parent.document.URL以警报形式返回。我假设你使用的是一个iFrame,否则它只是window.location.href,这是我在测试上面的代码时没有使用iFrame时所使用的。

在iFrame parent.location.href中通常会有这个技巧。

希望上次编辑,做了一些测试,并使用您的代码导致与您报告的错误相同的错误。我假设你正在做一个iFrame内的东西,如果你不是你应该使用:

$.getScript("https://apis.google.com/js/plusone.js", function() { 
    $('#googleplus').html('<g:plusone href="'+window.location.href+'" size="tall"></g:plusone>'); 
}); 
+0

所以你有什么想法,我怎么能在ajax页面中添加'plusone button'? –

+0

仍然不能在阿贾克斯工作。但也谢谢。 –