Javascript 在HTML中使用PHP重定向到随机站点
我需要能够让用户点击一个按钮,并被重定向到一个随机页面 我尝试将PHP放入JavaScript中,并将其放入HTML中,如下所示:Javascript 在HTML中使用PHP重定向到随机站点,javascript,php,html,Javascript,Php,Html,我需要能够让用户点击一个按钮,并被重定向到一个随机页面 我尝试将PHP放入JavaScript中,并将其放入HTML中,如下所示: <script> <button onclick="var jsVar = "<?php $urls = array("www.site1.com", "www.site2.com", "www.site3.com","www.site4.com"); $url = $urls[array_rand($urls)]; header("L
<script>
<button onclick="var jsVar = "<?php
$urls = array("www.site1.com", "www.site2.com", "www.site3.com","www.site4.com");
$url = $urls[array_rand($urls)];
header("Location: http://$url"); ?>"">Click</button>
</script>
PHP脚本将生成随机URL,当你点击按钮时,它将调用randsite($URL)
JavaScript函数,该函数将把你重定向到随机站点
<?php
$urls = array("http://www.site1.com", "http://www.site2.com", "http://www.site3.com","http://www.site4.com");
// select random url
$rand = $urls[mt_rand(0, count($urls) - 1)];
?>
<button onclick="randsite(<?php echo "'".$rand."'"; ?>)">Click</button>
<script type="text/javascript">
function randsite($url){
window.location = $url;
}
</script>
PHP脚本将生成随机URL,当你点击按钮时,它将调用randsite($URL)
JavaScript函数,该函数将把你重定向到随机站点
<?php
$urls = array("http://www.site1.com", "http://www.site2.com", "http://www.site3.com","http://www.site4.com");
// select random url
$rand = $urls[mt_rand(0, count($urls) - 1)];
?>
<button onclick="randsite(<?php echo "'".$rand."'"; ?>)">Click</button>
<script type="text/javascript">
function randsite($url){
window.location = $url;
}
</script>
试试这个
<?php
$urls = array("www.site1.com", "www.site2.com", "www.site3.com","www.site4.com");
$url = $urls[array_rand($urls)];
?>
<button onclick="myfunction();">Click</button>
<script>
function myfunction(){
var href = "<?php echo $url?>";
window.location.href = "http://"+href;
}
</script>
点击
函数myfunction(){
var href=“”;
window.location.href=“http://”+href;
}
试试这个
<?php
$urls = array("www.site1.com", "www.site2.com", "www.site3.com","www.site4.com");
$url = $urls[array_rand($urls)];
?>
<button onclick="myfunction();">Click</button>
<script>
function myfunction(){
var href = "<?php echo $url?>";
window.location.href = "http://"+href;
}
</script>
点击
函数myfunction(){
var href=“”;
window.location.href=“http://”+href;
}
PHP+HTML+JS:
<?php $url = "http://....."; ?>
<button name="redirect"onclick="redirectFunc(<?php echo $url; ?>);">Redirect with button</button>
<script>
function redirectFunc($url){
window.location.href = "<?php echo $url?>";
}
</script>
<button id="buttonID">redirect</button>
<script type="text/javascript">
// here you can rand your urls and choose one of them to redirect
document.getElementById("buttonID").onclick = function () {
location.href = "http://...";
};
</script>
PHP+HTML+JS:
<?php $url = "http://....."; ?>
<button name="redirect"onclick="redirectFunc(<?php echo $url; ?>);">Redirect with button</button>
<script>
function redirectFunc($url){
window.location.href = "<?php echo $url?>";
}
</script>
<button id="buttonID">redirect</button>
<script type="text/javascript">
// here you can rand your urls and choose one of them to redirect
document.getElementById("buttonID").onclick = function () {
location.href = "http://...";
};
</script>
用一个按钮做一个表单然后当按钮被点击时转到随机页面你可以用Javascript做这个,你根本不需要php用一个按钮做一个表单然后当按钮被点击时转到随机页面你可以用Javascript做这个,你根本不需要php它不起作用,但是谢谢!没用,但谢谢!