问题
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.
回答1:
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();
回答2:
I’ve done it this ways:
Install Joomla extension “jumi” @ url http://extensions.joomla.org/extensions/extension/core-enhancements/coding-a-scripts-integration/jumi
Create a html file(ex: back.html) and write following code:
<script>
function goBack()
{
window.history.back()
}
</script>
<input type="button" value="Back" onclick="goBack()" />
Save it into Joomla root directory.
Create a custom module in Joomla and write syntax :
{jumi [back.html]}
- Publish this module
I hope it’ll work. thanks
来源:https://stackoverflow.com/questions/12948109/return-to-previous-page-in-joomla