0

I have a little issue with javascript.

I have a page and this page is abc.com (page A) then i have a link in this page, redirecting to def.com (page B) and when the user click in this link, I need the page def.com load, and so I need these (def.com) page redirects to ghi.com (page C) after 3 seconds. I tried some codes, but I can't solve it by myself, someone can help me?

 document.addEventListener("DOMContentLoaded", function twoLinks() {
        myTab = document.getElementById("redirect").onclick = setTimeout(function () {
            document.getElementById("redirect").href = "http://www.def.com";
        }, 1000); 
        if(myTab){ 
            setTimeout(function(){ 
            window.location('http://www.ghi.com/');
            }, 3000); 
        }     
    });

Thanks in advance!

Edit: What I need is when the user click the link in PAGE A, the browser load the PAGE B and after 3 seconds they are redirected to PAGE C, all the pages at the same tab, but I don't have access to the page B or page C so, I can't put redirect links on then.

James F.
  • 21
  • 2
  • Does this answer your question? [How do I redirect to another webpage?](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) – VpsOP Jan 07 '21 at 08:19
  • Within the existing tab, you won’t be able to achieve this at all - after def.com has loaded, your own code is completely out of the picture, you have no access to or control over the tab any more. If anything, this could work if you open a _new_ tab, and keep a reference to it (window.open) - then you should still be able to set a new URL to navigate to for that tab, from the outside. – CBroe Jan 07 '21 at 08:21

2 Answers2

1

In abc.com put this

<a href="def.com" >Abc page </a>

On def.com put this

 setTimeout(function(){ window.location.replace("ghi.com"); }, 3000);

This is very dirty code - bat you can tray

<html>
<head>
<script type="text/javascript">

function changeUrl() {
    let site = "def.com";
    document.getElementsByName('cake')[0].src = site;
    let  frame = document.getElementById("itest");
    frame.style.display = '';
    let  button  = document.getElementById("btest");
    button.style.display = 'none';  
    setTimeout(function(){ window.location.replace("ghi.com"); }, 3000);
   
}
</script>
</head>

<body>
<center>

<button id="btest" onclick="changeUrl()">Load def.com</button>

<div>
    <iframe id="itest" src="" height="100%" width="100%" frameborder=0 name = "cake" style ="display:none"></></iframe>
</div>

</center>


</body>
WiatroBosy
  • 1,076
  • 1
  • 6
  • 17
-2

LOL dirty indeed, go for this it's clean and okay, okaaaaaaaaay.

  <script>
  setTimeout(function () {
  window.location.href = "http://www.ghi.com";
  }, 3000);
</script>