如何为 jQuery load() 内容制作自定义后退按钮

How to make a custom Back button for jQuery load() content

在我的 php 页面之一 (Report.php) 中,我正在通过 jQuery load() 加载其内容。我想在我的 php 页面上创建一个“后退按钮”,以便我的用户可以单击它返回页面。 注意- 我不是在寻找浏览器后退按钮。

这是我的 php 页 -

report_template.php

//This page query my database and creates a content table<br>
//Table content
<a href="#" onclick="fetchReport(<?php echo $row['report_id']; ?>);">Another Report Link</a>

report.php

<button>Go Back</button> //This button will take user to back (equivalent to window.history.back() )
<div id="rept"> </div>

<script>
jQuery('#rept').load('report_template?id=1');
 
function fetchReport(id){
 jQuery('#rept').load('report_template?id='+id);
}
</script>

我在 jquery/javascript 中处于中等水平。

在每个页面的顶部启动会话 session_start(); 并将当前页面设置为 $_SESSION 变量。浏览器

<?php
session_start();
$_SESSION['last_page'] = $_SERVER['PHP_SELF']; // Or whatever you designate it as

然后就可以在report.php

中调用了
<?php
session_start();

<button onClick="window.open(<?= $_SESSION['last_page'] ?>);">Go Back</button> //This button will take user to back (last report page he's seen)
<div id="rept"> </div>

<script>
jQuery('#rept').load('report_template?id=1');
 
function fetchReport(id){
 jQuery('#rept').load('report_template?id='+id);
}
</script>

一种稍微简单但不太可靠的方法是根据 PHP $_SERVER['HTTP_REFERER'] 抓取此人访问的最后一页。这也可以包括“离线”页面。如果推荐人实际上可能来自“其他地方” IE,它也可能更适合您的问题:

 <button onClick="window.open(<?= $_SERVER['HTTP_REFERER'] ?>);">Go Back</button>