我正在尝试从 jQuery 将 reCaptcha 代码插入到我的页面中,但它不起作用。
这是我的代码:
$("#button").click(function() {
$("#loginBox").html($.getRecaptcha());
})
当我尝试以下操作时,我的代码甚至无法运行。我认为它试图执行而不是渲染它。
$.getRecaptcha = function(){
return '<script type="text/javascript"' +
'src="http://api.recaptcha.net/challenge?' +
'k=6Ld3iAsAAAAAAGyX8QT244GagPEpCDSD-96o4gEi"></script>';
}
通过以下内容,代码运行,但是当我单击 #button 时,我得到一个空的全白色
浏览器窗口。
$.getRecaptcha = function(){
var captcha = $("<script>")
.attr("type", "text/javascript")
.attr("src", "http://api.recaptcha.net/challenge?k=6Ld3iAsAAAAAAGyX8QT244GagPEpCDSD-96o4gEi");
return captcha;
}
我不想从一开始就将 reCaptcha 代码插入到我的 html 中,因为即使我的用户不想使用它,它也会从 reCaptcha 服务器下载 reCaptcha 内容。我可以将保存 reCaptcha 的容器的可见性设置为不可见(显示:无;),但无论内容如何,它都会下载内容。
我可以插入reCaptcha给我们的“noScript”代码,但它也不起作用,因为它可以发现浏览器允许javascript,但我使用noScript。
有什么建议吗?
(我知道我将我的公共 reCaptcha 密钥放入代码中,但这只是出于测试目的,无论如何您都可以从我的 html 或 javascript 代码中获取它)
更新:
Krunal Mevada 回答了我的问题。但它不适用于 reCaptcha。 reCaptcha 提供了 reCaptcha AJAX API 这是我的具体问题的答案。但是,使用 getScript(),我可以动态下载 reCaptcha AJAX API javascipt 文件。
I'm trying to insert reCaptcha code into my page from jQuery, but it doesn't work.
Here is my code:
$("#button").click(function() {
$("#loginBox").html($.getRecaptcha());
})
When I try the following, my code doesn't even run. I think it's trying to execute instead of rendering it.
$.getRecaptcha = function(){
return '<script type="text/javascript"' +
'src="http://api.recaptcha.net/challenge?' +
'k=6Ld3iAsAAAAAAGyX8QT244GagPEpCDSD-96o4gEi"></script>';
}
With the following, the code runs, but when I click to #button I get an empty, full white
browser window.
$.getRecaptcha = function(){
var captcha = $("<script>")
.attr("type", "text/javascript")
.attr("src", "http://api.recaptcha.net/challenge?k=6Ld3iAsAAAAAAGyX8QT244GagPEpCDSD-96o4gEi");
return captcha;
}
I don't want to insert reCaptcha code into my html from the beginning, because it downloads the reCaptcha content from the reCaptcha server even if my users don't want to use it. I could set the visibility of the container that holds the reCaptcha to invisible (display: none;), but it will dowload the content irrespectively of it.
I can insert the "noScript" code that reCaptcha gives us, but it doesn't work either, because it can spot that the browser allow javascript but I use noScript.
Any suggestions?
(I know I put my public reCaptcha key into the code, but it is just for testing purposes, and you could get it from my html or javascript code anyway)
UPDATE:
Krunal Mevada answered my question. But it doesn't work with reCaptcha. reCaptcha offers a reCaptcha AJAX API which is the answer to my specific question. However, with getScript() I can dinamically download the reCaptcha AJAX API javascipt file.
发布评论
评论(2)
使用
getScript
方法jQuery。可能这个答案可能有用:
堆栈答案
Use
getScript
method of jQuery.May be this answer can be useful:
Stack Answer
第一个代码部分:在第一行末尾插入一个空格:
return '
First code part: insert a space at the end of the first line:
return '<script type="text/javascript" ' +