Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

javascript - Onload function not working for a href

I have a script:

<script>
function load_thankyou()
{
    alert('hii');
    var delay = 1000; //Your delay in milliseconds
    var URL = 'thankyou.php';
    setTimeout(function()
                { 
                  window.location = URL; 
                }, delay);
}
</script>

The function is called in the following manner...

<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') 
    {
        if($error_flag == 0)
        {?>

    <a class="close" href="#" onload="load_thankyou()">×</a>
<?php
        }
    }?>

But it seems, somehow the onload() function is not working..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you could do is to give your link only a class and href to # (what links to none) like:

<a class="close" href="#" >×</a>

And your JS could look like:

<script>

    $(document).ready(function(){
        //check if close element exists. If yes, execute the function
        if($('.close').length() > 0) load_thankyou();
    });

    function load_thankyou()
    {
        alert('hii');
        var delay = 1000; //Your delay in milliseconds
        var URL = 'thankyou.php';
        setTimeout(function()
                    { 
                      window.location = URL; 
                    }, delay);
    }

</script>

this waits until page is load and check if the a-tag exists and if yes it will execute the function. kind of like a onload for a-tags


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...