MediaWiki:Common.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
// JavaScript to dynamically create and insert a table, only on the "Meals" page
mw.loader.using(["mediawiki.util"]).then(function () {
(function () {
var targetPage = "Meals"; // Target page name
var currentPage = mw.config.get("wgPageName");
// Run the script only if the current page matches the target page
if (currentPage !== targetPage) {
return;
}
// Check if the content area exists
var contentArea = document.getElementById("mw-content-text");
if (!contentArea) {
return;
}
// Create table element
var table = document.createElement("table");
table.className = "wikitable"; // Apply MediaWiki's table styling
table.style.width = "50%";
table.style.margin = "20px auto";
// Add table header
var headers = ["Food", "Category", "Calories"];
var headerRow = document.createElement("tr");
headers.forEach(function (header) {
var th = document.createElement("th");
th.textContent = header;
headerRow.appendChild(th);
});
table.appendChild(headerRow);
// Add table rows with sample data
var data = [
["Pizza", "Fast Food", "285"],
["Apple", "Fruit", "52"],
["Salad", "Healthy", "150"]
];
data.forEach(function (row) {
var tableRow = document.createElement("tr");
row.forEach(function (cell) {
var 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 the table at the top of the content area
})();
});