首页 > 解决方案 > 如何从禁用 javascript 的页面重定向到上一页?

问题描述

我在主页上,并且我的 javascript 已启用。现在我做了什么,我禁用了javascript并刷新home.php页面,然后页面重定向javascript-disabled.php因为我添加了带有 URL 的元标记。到这里为止没有问题。

我的问题是,

我在javascript-disabled.php了,我启用了 javascript 并刷新了页面,但它没有在上一页上重定向。

这是禁用 javascript 时我的 URL。

http://localhost/example/javascript-disable.php

启用 javascript 并刷新页面后,我得到了这个 URLhttp://localhost/example/undefined

我添加了

<script type="text/javascript">
     window.location.href =  window.history.back();
</script>

主页.php

<!DOCTYPE html>
<html>
<head>
    <title>home</title>
     <noscript><meta http-equiv="refresh" content="0; url=javascript-disable.php" /></noscript>
</head>
<body>

<div>
    <!--content her-->
    hi
</div>

</body>
</html>

Javascript-disable.php

<!DOCTYPE html>
<html>
<head>
    <title>Javascrit disable</title>
</head>
<body>

<div>
        <h2>Javascript is disabled.</h2>
        <p>Please enable javascript and refersh the page.</p>
        <p>To unable to javascript click <a href="#" target="_blank">here</a></p>
</div>

<script type="text/javascript">
</script>
<script type="text/javascript">
     window.location.href =  window.history.back();
</script>
</body>
</html>

标签: javascriptphphtmlbrowser

解决方案


最后,我找到了我的答案,我不知道它是否最好。请建议它是否安全。

我做了什么,我在每个页面上添加了以下代码

session_start();
$_SESSION['url'] = $_SERVER['REQUEST_URI'];//It will store the page name

以及我在顶部添加的 javascript-disabled.php 页面

session_start();
$url = $_SESSION['url']; // holds url for last page visited.

在关闭 body 标签之前的底部,我添加了

<script type="text/javascript">
var previous_page= '<?php echo $url; ?>';
window.location.href = previous_page;
</script>

它运行良好。


推荐阅读