Javascript 当你点击锚标签时,如何将一个url重定向到另一个?
我需要一个单一的代码,当我点击锚标签,它重定向到一个URL,一段时间后,它重定向到另一个URL 一种在单个代码上的双url重定向 我试图操纵此代码进行双url重定向。在第一页:Javascript 当你点击锚标签时,如何将一个url重定向到另一个?,javascript,php,Javascript,Php,我需要一个单一的代码,当我点击锚标签,它重定向到一个URL,一段时间后,它重定向到另一个URL 一种在单个代码上的双url重定向 我试图操纵此代码进行双url重定向。在第一页: <a href="second_page.php?redirect=true>Link</a> 第二页: <?php if(isset($_GET['redirect']) && $_GET['redirect'] == true){ sleep
<a href="second_page.php?redirect=true>Link</a>
第二页:
<?php
if(isset($_GET['redirect']) && $_GET['redirect'] == true){
sleep(2);
header("Location: http://example.com/third_page.php");
}
有一种双url重定向的方法
<!DOCTYPE html>
<html>
<body>
<a href="#" onclick="window.open('http://google.com');
window.open('https://www.w3schools.com/html/');">Click to open Google and Yahoo</a></p>
</body>
</html>
如果要导航或添加一些时间,请在中创建一个函数,并为第二次导航应用超时方法
//案例2,以便在一段时间后打开第二个url
<!DOCTYPE html>
<html>
<body>
<a href="#" onclick=myFunction()>Click to open Google and Yahoo</a></p>
<script>
function myFunction(){
myWindow = window.open('http://google.com');
if(myWindow){ // if its null means the 1 one is not opened hence second //will not open
setTimeout(function(){
window.open('https://www.w3schools.com/html/');
}, 1000);
}
}
</script>
</body>
</html>
函数myFunction(){
myWindow=窗口。打开('http://google.com');
if(myWindow){//如果它的null表示1未打开,则第二个//将不会打开
setTimeout(函数(){
打开窗户https://www.w3schools.com/html/');
}, 1000);
}
}
不清楚您希望解决方案使用哪种语言。沿着PHP html javascript的链条,有3种方法(可以组合)。各有利弊
最简单的html方法是使用meta标记,其中content属性包含重定向时间(以秒为单位)和目标:
<meta http-equiv="refresh" content="5;URL='https://destination.com/index.html'" />
第三种方法是使用Javascript
window.location.href = "https://destination.com/index.html";
一个简单的例子(将三者结合起来,一起玩)
一个小提示,对于一个纯html的解决方案,2个重定向需要2个文件。JS和PHP允许您使用相同的文件,方法是向url添加一个计数器并从中读取(添加?counter=x)
const urlParams=新的URLSearchParams(window.location.search);
var counter=urlParams.get('counter');
var redir=“2.html”;
计数器++;
如果(计数器==2){
redir=“3.html”;
}
window.location.href=”https://destination.com/“+重拨+计数器;
您好,请先阅读如何提问。很难理解您想要什么。谢谢,它起作用了。。但我想在一个新的选项卡中重定向这两个链接。第二个链接应该在几秒钟后打开。“你能帮我吗?”尼克,正如我提到的,你需要添加超时功能。所以编辑了postHi,第二个链接作为弹出窗口打开。。。像我一样,许多人在他们的系统或手机中屏蔽弹出窗口。所以,是有可能打开第二个链接没有弹出窗口。
window.location.href = "https://destination.com/index.html";
<?php
$counter = isset($_GET['counter']) ? 1 : $_GET['counter'] +1;
$redit = "1.html";
if ($counter == 2) $redir = "3.html";
header('Location: https://destination.com/' . $redir . '?counter=' . $counter);
?>
<html>
<head>
<meta http-equiv="refresh" content="5;URL='https://destination.com/index.html'" />
</head>
<body>
<a href="2.html">click me</a>
<script type="text/Javascript">
const urlParams = new URLSearchParams(window.location.search);
var counter = urlParams.get('counter');
var redir = "2.html";
counter++;
if (counter == 2) {
redir = "3.html";
}
window.location.href = "https://destination.com/" + redir + counter;
</script>
</body>
</html>