简体   繁体   English

执行代码AFTER Recaptcha.reload()完成后

[英]execute code AFTER Recaptcha.reload() is finished

I have a function below which is called to reload a recaptcha image. 我在下面有一个函数被调用来重新加载recaptcha图像。 It works, reloads the image but won't do anything after that. 它工作,重新加载图像,但之后不会做任何事情。 Basically the form is small that has this recaptcha on it so I've shrunk it and allowed for clicking to enlarge and all that. 基本上这个形式很小,有重复这个,所以我缩小了它,允许点击放大和所有这些。 If the person presses "get another captcha" which calls reloadCAP() it checks to see if it has the class of being the larger image. 如果该人按下“获取另一个验证码”并调用reloadCAP(),则会检查它是否具有更大图像的类。 if it does i need it to add that class and css back to the elements AFTER the new image has loaded but I can't seem to get it to work. 如果它确实我需要它添加该类和css回到新图像加载后的元素但我似乎无法让它工作。 Any ideas? 有任何想法吗?

function reloadCAP() {
    if($("#recaptcha_widget img").hasClass('largecap')) {
        Recaptcha.reload();
        $("#recaptcha_widget img").addClass('largecap');
        $('#recaptcha_image').css('height', '62px');
    } else {
        Recaptcha.reload();
    }
}

here's the html for this: 这是这个的HTML:

<div id="recaptcha_widget" class="formRow" style="display:none;">
    <span class="f_label">Enter Words Below:</span>
    <input type="text" class="setWidth" id="recaptcha_response_field" name="recaptcha_response_field" />

    <div class="cantread">
        <strong>Can't read this?</strong><br />
        <a href="javascript:reloadCAP()">Get another CAPTCHA</a>
    </div>

    <div id="recaptcha_image"></div> <!-- image loaded into this div -->
        <div class="clear"></div>
        <span class="smalltext">(click to enlarge)</span>

        <br clear="all" />
    </div>
<script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6LfzMMwSAAAAADV6D04jDE6fKwrJ57dXwOEW-vY3&lang=en"></script>
$("#recaptcha_widget img").one('load',function(){
    $("#recaptcha_widget img").addClass('largecap');
    $('#recaptcha_image').css('height', '62px');
});

This will put a one time only listener on the load event of the image that you are reloading and then executes the folowing code. 这将在您正在重新加载的图像的load事件上放置一次仅侦听器,然后执行以下代码。

I used .one() instead of .load() here because you don't want to attach a new listener every time you call reloadCAP() 我在这里使用.one()而不是.load() ,因为每次调用reloadCAP()都不想附加新的侦听器

Edit 编辑

Ok, so here's what I believe the issue is. 好的,所以这就是我认为的问题。 When you call Recaptcha.reload() it is removing the <img /> and replacing it with a new one. 当您调用Recaptcha.reload()它将删除<img />并将其替换为新的。 So when we are trying to attach the event it is getting removed as the image gets removed. 因此,当我们尝试附加事件时,它会在图像被删除时被删除。

What you need to do is place the class largecap on the recaptcha_image div and modify your css style to look like 你需要做的是将类largecap放在recaptcha_image div上并修改你的css样式

.largecap img {
  height: whatever;
  width: whatever;
}

It sounds like what you actually need is custom theming such that you can style the captcha/image/etc exactly as needed: https://developers.google.com/recaptcha/docs/customization 听起来您实际需要的是自定义主题,以便您可以根据需要设置验证码/图像/等样式: https//developers.google.com/recaptcha/docs/customization

If you do want to stick to your current implementation, you can hook into Recaptcha's built in (and undocumented) callback functions prior to calling Recaptcha.create(). 如果你想坚持当前的实现,你可以在调用Recaptcha.create()之前挂钩Recaptcha的内置(和未记录的)回调函数。

//Called after Recaptcha.reload() is finished loading
Recaptcha._alias_finish_reload = Recaptcha.finish_reload;
Recaptcha.finish_reload = function (challenge, b, c) {
    //Call original function that creates the new img
    Recaptcha._alias_finish_reload(challenge, b, c);

    $("#recaptcha_widget img").toggleClass('largecap', true);
}

//Called when the initial challenge is received on page load
Recaptcha._alias_challenge_callback = Recaptcha.challenge_callback;
Recaptcha.challenge_callback= function () {
    //Call original function that creates the new img
    Recaptcha._alias_challenge_callback();

    $("#recaptcha_widget img").toggleClass('largecap', true);
}

The reason you're even having this problem is because Recaptcha destroys and creates a new img everytime it reloads, so the styling you added manually will be lost. 你甚至遇到这个问题的原因是因为Recaptcha每次重新加载都会破坏并创建一个新的img,因此你手动添加的样式将会丢失。

Not the ideal solution, but you could put the addClass code above Recaptcha.reload() and just delay it by a second or two. 不是理想的解决方案,但您可以将addClass代码放在Recaptcha.reload()之上,并将其延迟一两秒。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM