Home Trouble Redirecting inside a function
Reply: 2

Trouble Redirecting inside a function

Milan1360
1#
Milan1360 Published in 2018-02-13 00:14:52Z

I have a simple animation I found online and modified to fit my needs, but no matter what I try, I cannot get it to redirect after the animation is done. If I remove the setTimeout It redirects immediately without showing the animation. This is my code:

<html>
<head>
<div id="x" style="background-color:rgb(10,10,10)" align="center">
<img src="icon.ico" alt="Icon" height="256px">
</div>
<script>
    var unBlue=105;
    var gEvent=setInterval("toWhite();", 10);
    var redirect=1;
    function toWhite(){
        if(unBlue<200) document.getElementById("x").style.backgroundColor="rgb("+unBlue+",30,30)";
        else clearInterval(gEvent)
        unBlue+=1;
        setTimeout(myFunction, 1000);
        location.redirect("home.html") //doesn't work
    }
</script>
</html>

I'm still getting used to javascript so a full explanation would be much appreciated. Thank you.

Dagan
2#
Dagan Reply to 2018-02-13 09:30:58Z

this code snippet works for me...

first as suggested use window.location.replace instead of location.redirect which does not seem to be supported

second myFunction does not seem to be defined.. you didn't specify if it does anything so if you just want to delay - I put null instead

<html>
    <head>
        <div id="x" style="background-color:rgb(10,10,10)" align="center">
            <img src="icon.ico" alt="Icon" height="256px">
        </div>
    <script>
        var unBlue=105;
        var gEvent=setInterval("toWhite();", 10);
        var redirect=1;
        function toWhite(){
            if(unBlue<200) document.getElementById("x").style.backgroundColor="rgb("+unBlue+",30,30)";
            else clearInterval(gEvent)
            unBlue+=1;
            setTimeout(null, 1000);
            window.location.replace("http://stackoverflow.com")
         }
    </script>
</html>
Gilberto Quintero A
3#
Gilberto Quintero A Reply to 2018-02-13 00:21:52Z

you need something how this ,is not need use Jquery :

For example:

// similar behavior as an HTTP redirect


    window.location.replace("http://example.com");

// similar behavior as clicking on a link

    window.location.href = "http://example.com";

more imformation :here

you code with the soluction :

<html>
<head>
<div id="x" style="background-color:rgb(10,10,10)" align="center">
<img src="icon.ico" alt="Icon" height="256px">
</div>
<script>
    var unBlue=105;
    var gEvent=setInterval("toWhite();", 10);
    var redirect=1;
    function toWhite(){
        if(unBlue<200) document.getElementById("x").style.backgroundColor="rgb("+unBlue+",30,30)";
        else clearInterval(gEvent)
        unBlue+=1;
        setTimeout(myFunction, 1000);
        //location.redirect("home.html") //doesn't work
      window.location.href = "http://example.com";
    }
</script>
</html>

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.473752 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO