如何从禁用javascript的页面重定向到上一页?

时间:2018-05-05 15:18:55

标签: javascript php html browser

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

我的问题是,

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

这是我禁用javascript时的网址。

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

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

我添加了

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

Home.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>

1 个答案:

答案 0 :(得分:1)

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

我做了什么,我在每页的代码中添加了以下代码

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>

并且它运作良好。