Actions

MediaWiki

Common.js: Difference between revisions

An Avatar.Global Resource

No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
mw.hook('wikipage.content').add(function($content) {
 
     var $headline = $content.find("h1, h2").first();
$(document).ready(function() {
     if ($headline.length) {
    // Get the last visited page from localStorage
         $("<div class='custom-message'>{{HeaderMessage}}</div>").insertBefore($headline);
     var lastPage = localStorage.getItem('lastVisitedPage');
 
    // Get the current page title
    var currentPage = mw.config.get('wgPageName').replace(/_/g, ' ');
 
    // Store the current page in localStorage for the next visit
    if (currentPage !== "Main_Page") { // Avoid setting breadcrumbs for the main page
        localStorage.setItem('lastVisitedPage', currentPage);
    }
 
    // Display the breadcrumb if a last visited page exists
     if (lastPage && lastPage !== currentPage) {
         var breadcrumbHtml = `<div class="breadcrumb" style="margin-bottom:10px;">
            <a href="/wiki/${encodeURIComponent(lastPage)}">← Back to ${lastPage}</a>
        </div>`;
 
        // Insert breadcrumb at the top of the page content
        $('#content').prepend(breadcrumbHtml);
     }
     }
});
});

Revision as of 13:45, 16 March 2025

mw.hook('wikipage.content').add(function($content) {
    var $headline = $content.find("h1, h2").first();
    if ($headline.length) {
        $("<div class='custom-message'>{{HeaderMessage}}</div>").insertBefore($headline);
    }
});