Tumblr主题自定义:文章页添加返回上一页链接问题求助
Hey there! Let's get that back-to-previous-page link working properly for your custom Tumblr theme. I see you've tried a couple of approaches, so let's break down what's going wrong and how to fix it.
Why Your Current Code Isn't Working
- Your first link (
<a class="acme-post-return -hide-index" href="/">← Back to {Title}</a>) always redirects to your blog's homepage, not the actual previous page the user came from. That's why it doesn't match the behavior you want. - Your second link has a syntax issue in the
onclickattribute—you're missing proper quoting around the JavaScript, which breaks the functionality. Even if fixed, relying solely ondocument.referrercan be unreliable (it might be empty if the user landed directly on the post page, for example).
The Reliable Solution
Use the browser's built-in window.history.back() method, which directly navigates to the previous page in the user's browsing history. Here's the corrected code:
<a class="acme-post-return -hide-index" onclick="window.history.back(); return false;" href="/">← Go to Previous Page</a>
What This Does:
window.history.back()tells the browser to go back one page, just like clicking the browser's back button.return false;prevents the default link behavior (which would take the user to/if the JavaScript fails).- The
href="/"acts as a fallback, so if for some reason the history back doesn't work (e.g., the user opened the post in a new tab), they'll still land on your homepage.
Bonus: Add a Fallback for Empty History
If you want to make it even more robust (for users who land directly on the post page with no browsing history), you can check if there's a previous page to go back to:
<a class="acme-post-return -hide-index" onclick=" if (window.history.length > 1) { window.history.back(); } else { window.location.href = '/'; } return false; " href="/">← Go to Previous Page</a>
Quick Check
Make sure this code is placed within your Tumblr theme's post page template (usually inside the {block:PermalinkPage} block) so it only shows up when someone is viewing an individual post. Your existing -hide-index class should already handle hiding it on the homepage, which is perfect.
内容的提问来源于stack exchange,提问作者Lorento L

