'JS - How can I open a url and then redirect to another url after 3 seconds in the same tab
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.
Solution 1:[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>
Solution 2:[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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | kevin igwilo |