因为到处都找不到这个问题的答案,所以问题来了。但在此之前,感谢任何回答/帮助的人。
index.php页面的伪代码是:
<html>
<head><script>
<?php
$links = parse_ini_file('links.ini');
if(isset($_GET['l']) && array_key_exists($_GET['l'], $links)){
$my_phpvar = $links[$_GET['l']];
}
else{
header('HTTP/1.0 404 Not Found');
echo 'Unknown link.';
}
?>
var myjsvar= <?php echo $my_phpvar; ?>
function go(){
document.cookie = "visited=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
window.location.href = "myjsvar";
}
</script></head>
<body><a id="myA1" href="javascript:go();" target="_blank">Click</a></body>
</html>
很明显,在上面的代码中,myjsvar 来自 my_phpvar,而 my_phpvar 来自一个单独的文件 links。 ini(对不起,如果我让你感到厌烦,因为它在代码中很明显,但我不想错过任何可以提供帮助的人)
我在 index.php 所在目录的根目录中的 .htaccess 文件中添加了一些规则。添加的规则是
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?l=$1 [L]
links.ini 文件如下所示:
ex = https://www.example.com
所以主要问题是:当我浏览 URL http://www.yoursite.com/short/index.php?l=ex 时 ,然后单击按钮启动 function go(),它不会将我带到网站 https://www.example.com
再次感谢解决/帮助解决问题的任何人。
最佳答案
用引号将 jsvar
括起来:
var myjsvar = "<?php echo $my_phpvar; ?>";
以后,将其用作变量(而不是 sring):
window.location.href = myjsvar;
关于javascript - 使用 PHP 解析 URL 并将其作为变量提供给 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050949/