开发者

How to call the function in this file?

I pasted the code given in this link to a file called md5.js.

http://www.webtoolkit.info/javascript-md5.html

I am not able to call the function in my below code. Please assist me.

function inc(filename) { var body = document.getElementsByTagName('body').item(0); script = document.createElement('script'); script.src = filename; script.type = 'text/javascript'; body.appendChild(script) } function CheckCaptcha() { var Captch开发者_开发知识库aWord=""; CaptchaWord = document.getElementById('studentusername').value; inc("md5.js"); //Add MD5 function here. }


You can try adding an event handler for the scripts onload and continuing your code from there.

e.g.

function inc(fname, callback)
{
    var body = document.getElementsByTagName('body').item(0);
    script = document.createElement('script');
    script.src = filename;
    script.type = 'text/javascript';
    script.onload = callback;
    body.appendChild(script);
}

function CheckCaptcha()
{
    var CaptchaWord="";

    CaptchaWord = document.getElementById('studentusername').value;
        inc("md5.js", function() {
        //Add MD5 function here.
        });

}

The alternative to this approach (which will work far more rebustly as well) is to include the md5 script directly as opposed to using the inc function.

<script src="/path/to/md5.js" type="text/javascript"></script>
<script type="text/javascript">
function CheckCaptcha()
{
    var CaptchaWord="";
    CaptchaWord = document.getElementById('studentusername').value;
    return md5(CaptchaWord); //or something
}
</script>


As onload doesnt work in ie you need to add onreadystatechange.

function inc(fname, callback)
{
    var body = document.getElementsByTagName('body').item(0);
    script = document.createElement('script');
    script.src = filename;
    script.type = 'text/javascript';
    script.onload = callback;
    script.onreadystatechange= function () {
      if (this.readyState == 'complete') callback();
    }

    body.appendChild(script);
}

function CheckCaptcha()
{
    var CaptchaWord="";

    CaptchaWord = document.getElementById('studentusername').value;
        inc("md5.js", function() {
        //Add MD5 function here.
        });

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜