J3 Issue ?
avatar Spudley
Spudley
28 Jan 2016

Steps to reproduce the issue

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

Expected result

  • 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)

Actual result

  • The URL in the anchor tag gets rewritten by Joomla when the page is rendered so it includes the current page URL. But crucially it misses out the URL arguments, so the browser (legitimately) sees it as a different URL to the current page. Thus clicking on it does result in a page reload and the page is loaded without any arguments, thus losing any search results, etc we had on screen.

System information (as much as possible)

  • Joomla 3.4.8.
  • Global config: Friendly URLs, URL Rewriting and Add suffix options all enabled.

Additional comments

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.

avatar Spudley Spudley - open - 28 Jan 2016
avatar fruppel
fruppel - comment - 28 Jan 2016

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.

The HTML <base> element specifies the base URL to use for all relative URLs contained within a document.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

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:

/**
 * Returns the URL for the request, minus the query.
 *
 * @return  string
 *
 * @since   11.1
 */
public static function current()

Is this intended behaviour or a bug? A solution could be to change

    $document->setBase(htmlspecialchars(JUri::current()));

to

    $document->setBase(htmlspecialchars(JUri::getInstance()));
avatar brianteeman
brianteeman - comment - 8 Apr 2016

Based on the docblock I would say it is the intended behaviour


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9010.

avatar Spudley
Spudley - comment - 8 Apr 2016

Intended or not, it does seem to be the root cause of the bug.

avatar bertmert
bertmert - comment - 8 Apr 2016

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.

avatar brianteeman brianteeman - close - 13 Apr 2016
avatar brianteeman brianteeman - change - 13 Apr 2016
Status New Expected Behaviour
Closed_Date 0000-00-00 00:00:00 2016-04-13 09:34:44
Closed_By brianteeman
avatar brianteeman
brianteeman - comment - 13 Apr 2016

Closing as expected behaviour


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9010.

avatar brianteeman brianteeman - close - 13 Apr 2016
avatar Spudley
Spudley - comment - 16 Apr 2016

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

avatar izharaazmi
izharaazmi - comment - 27 Jun 2018

@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?

avatar joomla-cms-bot joomla-cms-bot - change - 27 Jun 2018
The description was changed
Status Expected Behaviour New
Closed_Date 2016-04-13 09:34:44
Closed_By brianteeman
avatar joomla-cms-bot joomla-cms-bot - reopen - 27 Jun 2018
avatar joomla-cms-bot joomla-cms-bot - change - 27 Jun 2018
The description was changed
Status New Expected Behaviour
avatar joomla-cms-bot joomla-cms-bot - edited - 27 Jun 2018
avatar franz-wohlkoenig franz-wohlkoenig - change - 27 Jun 2018
Status Expected Behaviour New
Closed_Date 0000-00-00 00:00:00
avatar joomla-cms-bot joomla-cms-bot - edited - 27 Jun 2018
avatar joomla-cms-bot
joomla-cms-bot - comment - 27 Jun 2018
avatar franz-wohlkoenig franz-wohlkoenig - change - 27 Jun 2018
Status New Discussion
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 27 Jun 2018

Reopened as stated above.

avatar izharaazmi
izharaazmi - comment - 27 Jun 2018
avatar csthomas
csthomas - comment - 27 Jun 2018

You can put in your template index.php file, somewhere at the top this line:

JFactory::getDocument()->setBase('');
avatar brianteeman
brianteeman - comment - 27 Jun 2018

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.

avatar brianteeman brianteeman - change - 27 Jun 2018
Status Discussion Closed
Closed_Date 0000-00-00 00:00:00 2018-06-27 14:30:34
Closed_By brianteeman
avatar brianteeman brianteeman - close - 27 Jun 2018
avatar brianteeman
brianteeman - comment - 27 Jun 2018

oops ddidnt mean to press close just then

avatar brianteeman brianteeman - change - 27 Jun 2018
Status Closed New
Closed_Date 2018-06-27 14:30:34
Closed_By brianteeman
avatar brianteeman brianteeman - reopen - 27 Jun 2018
avatar franz-wohlkoenig franz-wohlkoenig - change - 28 Jun 2018
Status New Discussion
avatar wilsonge
wilsonge - comment - 18 Aug 2018

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.

avatar izharaazmi
izharaazmi - comment - 18 Aug 2018

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);
	});
});
avatar csthomas
csthomas - comment - 21 Aug 2018

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.

  • for https://www.joomla.org/ it should be https://www/joomla.org/
  • for https://www.joomla.org/menu_cat1/menu_cat2/cat3/1-article it should be still https://www/joomla.org/
  • for my joomla installation at 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
avatar brianteeman brianteeman - change - 30 Oct 2018
Labels Added: J3 Issue
avatar brianteeman brianteeman - labeled - 30 Oct 2018
avatar Milglius
Milglius - comment - 23 Mar 2019

is it fixed now? relaated maybe to #23316

avatar franz-wohlkoenig
franz-wohlkoenig - comment - 27 Mar 2019

@HLeithner can you please comment?

avatar HLeithner
HLeithner - comment - 25 Apr 2019

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.

avatar joomla-cms-bot joomla-cms-bot - change - 31 May 2019
Status Discussion Closed
Closed_Date 0000-00-00 00:00:00 2019-05-31 21:11:55
Closed_By joomla-cms-bot
avatar joomla-cms-bot joomla-cms-bot - close - 31 May 2019
avatar Quy Quy - change - 31 May 2019
Closed_By joomla-cms-bot Quy
avatar joomla-cms-bot
joomla-cms-bot - comment - 31 May 2019

Set to "closed" on behalf of @Quy by The JTracker Application at issues.joomla.org/joomla-cms/9010

avatar Quy
Quy - comment - 31 May 2019

Please test PR #25077


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/9010.

avatar Hackwar
Hackwar - comment - 31 May 2019

That was quick. For all who followed this issue: After some carefull consideration, it simply seems as if this tag is not necessary and should simply be removed. To confirm, please test #25077. Thank you!

Add a Comment

Login with GitHub to post a comment