MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
(Created page with "→Any JavaScript here will be loaded for all users on every page load.: // JavaScript to dynamically create and insert a table into a MediaWiki page (function () { // Check if the page content area exists const contentArea = document.getElementById("mw-content-text"); if (!contentArea) return; // Create table element const table = document.createElement("table"); table.className = "wikitable"; // MediaWiki styling class table.style.width = "50%"; tabl...") |
No edit summary |
||
| Line 1: | Line 1: | ||
// JavaScript to dynamically create and insert a table, only on a specific page | |||
// JavaScript to dynamically create and insert a table | |||
(function () { | (function () { | ||
// Specify the page where the script should run | |||
const targetPage = "Target_Page_Name"; // Replace with your page's title (spaces replaced with underscores) | |||
// Get the current page name from MediaWiki | |||
const currentPage = mw.config.get("wgMeals"); | |||
// Run the script only on the target page | |||
if (currentPage !== targetPage) return; | |||
// Check if the page content area exists | // Check if the page content area exists | ||
const contentArea = document.getElementById("mw-content-text"); | const contentArea = document.getElementById("mw-content-text"); | ||
Revision as of 17:57, 15 December 2024
// JavaScript to dynamically create and insert a table, only on a specific page
(function () {
// Specify the page where the script should run
const targetPage = "Target_Page_Name"; // Replace with your page's title (spaces replaced with underscores)
// Get the current page name from MediaWiki
const currentPage = mw.config.get("wgMeals");
// Run the script only on the target page
if (currentPage !== targetPage) return;
// Check if the page content area exists
const contentArea = document.getElementById("mw-content-text");
if (!contentArea) return;
// Create table element
const table = document.createElement("table");
table.className = "wikitable"; // MediaWiki styling class
table.style.width = "50%";
table.style.margin = "20px auto";
// Add table header
const headers = ["Name", "Age", "City"];
const headerRow = document.createElement("tr");
headers.forEach((header) => {
const th = document.createElement("th");
th.textContent = header;
headerRow.appendChild(th);
});
table.appendChild(headerRow);
// Add table rows with random data
const data = [
["Alice", "25", "New York"],
["Bob", "30", "Los Angeles"],
["Charlie", "35", "Chicago"]
];
data.forEach((row) => {
const tableRow = document.createElement("tr");
row.forEach((cell) => {
const td = document.createElement("td");
td.textContent = cell;
tableRow.appendChild(td);
});
table.appendChild(tableRow);
});
// Insert the table into the page content area
contentArea.prepend(table); // Add table at the top of the page content
})();