Return to previous page in joomla

content-management-system, joomla, joomla2.5

Solution

If I understand your question right, This will help you out for sure. Joomla does not support something like that but Javascript does.

JS

<script>
function goBack()
  {
  window.history.back()
  }
</script>

HTML

<input type="button" value="Cancel" onclick="goBack()" />

The back() method loads the previous URL in the history list. This is the same as clicking the Back button or history.go(-1).

Updated Answer as required.

PHP

If JavaScript isn't a possibility, you could use the HTTP_REFERER, sanitize it, and echo it out via PHP.

<?php
  $url = htmlspecialchars($_SERVER['HTTP_REFERER']);
  $this->setRedirect($url);
?>

This will not work if

- entered the site URL in browser address bar itself.

- visited the site by a browser-maintained bookmark.

- visited the site as first page in the window/tab.

- has security software installed (antivirus/firewall/etc) which strips the referrer from all requests.

- visited the site programmatically (like, curl) without setting the referrer header (searchbots!).

If you come across any problems let me know.

Update answer - Joomla method

$url = JFactory::getURI();
$request_url = $url->toString();

Problem

I have created my own extension. This extension can be accessed from different components and parts of the website. The problem I have is that there could be a number of different URLs that link to this extension. I have a 'cancel' button and I can't work out how I would get this button to link to the previous URL. Wondering if there is something built into joomla like getPreviousURL? Couldn't find anything. EDIT: I should have said that I still want to be able to run my cancel() method. e.g. index.php?option=com_mycomp&task=mycomp.cancel. The cancel() method will then perform a redirection to the previous page.

Original source