gpt4 book ai didi

javascript - 打开在 Javascript 中用 PHP 检索到的 url

转载 作者:行者123 更新时间:2023-11-30 12:06:57 24 4
gpt4 key购买 nike

需要使用 javascript 打开 $link,它是使用 PHP 从 XML 文件中检索到的。这是代码:

<?php

$url = "map.xml" ;
$xml = simplexml_load_file($url);
$link = $xml->url[mt_rand(0,count($xml->url)-1)]->loc ; // Get Random Location
?>

<html>
<head>
<title></title>
</head>
<body>
<script>
$(document).ready(function(){
window.open($link, "_blank"); // will open new tab on document ready
});
</script>
</body>
</html>

最佳答案

您不能从 javascript 访问 php 变量。使用 php,您可以在服务器上呈现 html 和 javascript。所以你需要打印你的 $link页面变量:

window.open("<?= $link ?>", "_blank");

注意短回显标签 <?=只保证在 php 5.4+ 上可用,如果你坚持支持旧版本使用 <?php echo相反

关于javascript - 打开在 Javascript 中用 PHP 检索到的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34907099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com