Using the Protostar template, or any other that contains anchor links (eg a href='#top').
Go to a page on your site that has URL parameters (querying something on the search page should do the trick)
Now try to use the anchor tag.
I was able to work around this by adding a call to JUri::getInstance() to the HTML template. So my #top HTML code now looks like this:
<a href="<?php echo JUri::getInstance(); ?>#top">Go to top</a>.
This seems to have resolved the problem, but is a work-around rather than a proper solution.
I initially thought this might be a problem with my installation or config or one of my plugins, but it does actually happen with a completely bare fresh Joomla install. It can be seen easily by creating a demo site at https://demo.joomla.org/, adding a search page to it, and then doing a search. On your search results page, clicking the "Back To Top" link at the bottom of the page will reload the page, stripped of all the URL arguments including the search term.
"Back to top" links like this appear in a lot of templates, so I'm surprised this hasn't been caught before.
Based on the docblock I would say it is the intended behaviour
Intended or not, it does seem to be the root cause of the bug.
It's not a Joomla bug but a "known issue" since websites exist.
That's why I use scroll-javascript (JQuery). You'll find some scroll-to-top plugins in JED, too.
Status | New | ⇒ | Expected Behaviour |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2016-04-13 09:34:44 |
Closed_By | ⇒ | brianteeman |
Closing as expected behaviour
@brianteeman: respectfully I disagree. Regardless of whether the code is intended to be the way it is, nobody would expect a "back to top" link to cause the URL arguments to be dropped, but that's what happens. It needs to be corrected.
I agree it doesn't necessarily warrant a change in JUri::current()
as that would functionally change a documented API function, but something needs to change. Maybe it just needs workaround I mentioned originally to be implemented in the default templates.
@brianteeman Expected behaviour… of what?
I understand that the behaviour for the method Uri::current()
is indeed an expected behaviour and of course we shouldn't change that.
But you see this issue is not about this method, its about the anchor links. And IMHO, that is NOT an expected behaviour. Can we please reopen this as we really need to find a fix for this?
Status | Expected Behaviour | ⇒ | New |
Closed_Date | 2016-04-13 09:34:44 | ⇒ | |
Closed_By | brianteeman | ⇒ |
Status | New | ⇒ | Expected Behaviour |
Status | Expected Behaviour | ⇒ | New |
Closed_Date | 0000-00-00 00:00:00 | ⇒ |
Set to "open" on behalf of @franz-wohlkoenig by The JTracker Application at issues.joomla.org/joomla-cms/9010
Status | New | ⇒ | Discussion |
Reopened as stated above.
Thanks @franz-wohlkoenig
You can put in your template index.php
file, somewhere at the top this line:
JFactory::getDocument()->setBase('');
From the original post
Expected Behaviour
The "go to top" anchor tag should jump you to the top of the current page without reloading the page. (other anchors would go wherever they point to on the current page, again without reloading)
That is not correct - an anchor tag generates an http request see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base#Hint
The usage of an anchor tag within the page, e.g.
<a href="#anchor">anchor</a>
is resolved by using the base url as reference and triggers an http request to the base url.
Status | Discussion | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2018-06-27 14:30:34 |
Closed_By | ⇒ | brianteeman |
oops ddidnt mean to press close just then
Status | Closed | ⇒ | New |
Closed_Date | 2018-06-27 14:30:34 | ⇒ | |
Closed_By | brianteeman | ⇒ |
Status | New | ⇒ | Discussion |
I think basically Joomla technically meets the specification of how this works but clearly it isn't expected behaviour either (for example this actually resets the pagination on people's sites when you hit back to top).
I don't think changing the base URL is correct as that does match the spec and is important for search engines etc. So I think the thing to do here is implement some basic js that overrides the link and shortcuts your browser to a page on the site.
Not sure if this is a good way, but I am now using this as a rescue:
jQuery(document).ready(function ($) {
$('body').on('click', 'a[href^="#"][href!="#"]', function (e) {
e.preventDefault();
var h = $(this).attr('href');
var a = 'a[name="' + h.substring(1) + '"]';
var d = $(a);
d.length || (d = $(h));
d.length && $('html, body').animate({
scrollTop: d.offset().top
}, 800);
});
});
IMO base tag generated by joomla 3 is broken in a different way.
For example go to https://www.joomla.org/3/component/users?view=login&Itemid=470
and take a look at
<base href="https://www.joomla.org/3/component/users" />
I do not understand why virtual path (3/component/users
) is added.
https://www.joomla.org/
it should be https://www/joomla.org/
https://www.joomla.org/menu_cat1/menu_cat2/cat3/1-article
it should be still https://www/joomla.org/
http://localhost/~csthomas/joomla3.8.11/
, if a link to article is http://localhost/~csthomas/joomla3.8.11/menu-1/my-article
then the base tag should be http://localhost/~csthomas/joomla3.8.11/
The solution could be
diff --git a/libraries/src/Application/SiteApplication.php b/libraries/src/Application/SiteApplication.php
index d679010218c..7a7bf28a3be 100644
--- a/libraries/src/Application/SiteApplication.php
+++ b/libraries/src/Application/SiteApplication.php
@@ -161,7 +161,7 @@ final class SiteApplication extends CMSApplication
if ($router->getMode() == JROUTER_MODE_SEF)
{
- $document->setBase(htmlspecialchars(\JUri::current()));
+ $document->setBase(htmlspecialchars(\JUri::base()));
}
// Get the template
Labels |
Added:
J3 Issue
|
@HLeithner can you please comment?
base parameter is broken and should be fixed. Base path should be the base url of the cms and not the current url.
We have the SEF plugin that tries to fix this with many regex replace voodoo.
Status | Discussion | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2019-05-31 21:11:55 |
Closed_By | ⇒ | joomla-cms-bot |
Closed_By | joomla-cms-bot | ⇒ | Quy |
Set to "closed" on behalf of @Quy by The JTracker Application at issues.joomla.org/joomla-cms/9010
Please test PR #25077
I think I found the problem. With SEF turned on, a
<base>
element is added in<head>
. But the href URL in this element is wrong (without the GET parameters). The browser uses this URL and adds the anchor.Example
Browser URL:
http://localhost/joomla/index.php/search?searchword=test&ordering=newest&searchphrase=all
<base href>
:http://localhost/joomla/index.php/search
Anchor link:
http://localhost/joomla/index.php/search#top
If you click the anchor you will be redirected to /search and lose all GET parameters.
The "wrong" URL gets set here:
https://github.com/joomla/joomla-cms/blob/3b1562b3b17fabd1688c6805c6170c1090280cb6/libraries/cms/application/site.php#L161
JUri::current() strips off GET parameters:
Is this intended behaviour or a bug? A solution could be to change
to