Link to reload page at latest post?

Started by samborabora, April 26, 2015, 02:08:25 PM

Previous topic - Next topic

samborabora

I know there's a static link that can take you to the latest post, but is it possible have a link that when you click it, grabs the latest post on whichever topic you are reading and refreshes you to it?

margarett

Apart from the permissions stuff, you just need to pick the last post in the topic you are currently viewing.
With it you just need to build the link as follows:
index.php?topicyyyy.msgxxxx#msgxxxx

Where yyyy is the ID of the topic and xxxx is the ID of the last post you just gathered.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

samborabora

Quote from: margarett on April 26, 2015, 06:20:09 PM
Apart from the permissions stuff, you just need to pick the last post in the topic you are currently viewing.
With it you just need to build the link as follows:
index.php?topicyyyy.msgxxxx#msgxxxx

Where yyyy is the ID of the topic and xxxx is the ID of the last post you just gathered.

That's what I thought, but what happens if people post on the topic in-between times, and the most recent post isn't the same as the page thought when it built the link? Is there a way of checking the actual newest post ID when the reload link is clicked, and loading the page from there?

margarett

Where do you want the link to show up?
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

samborabora


margarett

This works ;)
echo '
<a href="' . $scripturl . '?topic=' . $context['current_topic'] . '.msg' . $context['topic_last_message'] . '#msg' . $context['topic_last_message'] . '">THIS IS MY LINK </a>';
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

samborabora

Thanks! But, a couple of things, unfortunately, this isn't bringing the page down to the anchor at the bottom of the page, instead it is keeping it fixed at the top, and also, this is a static link, so if someone posts on the topic in-between times, this reload link will only take you to the last post made when the page was last refreshed.

Is it possible to have it dynamically fetch the latest post upon clicking the reload button and going to that post?

margarett

Hmmmm in my test board it does go to the latest post in the topic. Each message has in anchor, which is #msgxxxx so if your forum isn't doing that, something is weird with your theme

As for your second question, it should be doable but I don't see how out of my head...
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

samborabora

Well, I'd be interested if there is such a way to do this, HOWEVER, I came up with a cool way of reloading the current topic page without scrolling to the top:

Load this in the head of index.template
<script type="text/javascript" src="' . $settings['theme_url'] . '/scroll-sneak.js"></script>

Put this in scroll-sneak.js
/*!
* Scroll Sneak
* http://mrcoles.com/scroll-sneak/
*
* Copyright 2010, Peter Coles
* Licensed under the MIT licenses.
* http://mrcoles.com/media/mit-license.txt
*
* Date: Mon Mar 8 10:00:00 2010 -0500
*/
var ScrollSneak = function(prefix, wait) {
    // clean up arguments (allows prefix to be optional - a bit of overkill)
    if (typeof(wait) == 'undefined' && prefix === true) prefix = null, wait = true;
    prefix = (typeof(prefix) == 'string' ? prefix : window.location.host).split('_').join('');
    var pre_name;

    // scroll function, if window.name matches, then scroll to that position and clean up window.name
    this.scroll = function() {
        if (window.name.search('^'+prefix+'_(\\d+)_(\\d+)_') == 0) {
            var name = window.name.split('_');
            window.scrollTo(name[1], name[2]);
            window.name = name.slice(3).join('_');
        }
    }
    // if not wait, scroll immediately
    if (!wait) this.scroll();

    this.sneak = function() {
// prevent multiple clicks from getting stored on window.name
if (typeof(pre_name) == 'undefined') pre_name = window.name;

// get the scroll positions
        var top = 0, left = 0;
        if (typeof(window.pageYOffset) == 'number') { // netscape
            top = window.pageYOffset, left = window.pageXOffset;
        } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) { // dom
            top = document.body.scrollTop, left = document.body.scrollLeft;
        } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) { // ie6
            top = document.documentElement.scrollTop, left = document.documentElement.scrollLeft;
        }
// store the scroll
        if (top || left) window.name = prefix + '_' + left + '_' + top + '_' + pre_name;
        return true;
    }
}


Add this to Display.template:
echo'
<!-- SCROLL SNEAK JS: you can change `location.hostname` to any unique string or leave it as is -->
<script>
(function() {
    var sneaky = new ScrollSneak(location.hostname), tabs = document.getElementById("tabs").getElementsByTagName("a"), i = 0, len = tabs.length;
    for (; i < len; i++) {
        tabs[i].onclick = sneaky.sneak;
    }
    document.getElementById("tabs").onclick = sneaky.sneak;
})();
</script>
<!-- END OF SCROLL SNEAK JS -->
';


And finally, add id="tabs" to your reload link:
<a id="tabs" href="' . $message['href'] . '">[Reload]</a>

Advertisement: