all repos — retro-website @ main

res/js/frame-link-plain.js (view raw)

 1// Melo-tech Frame Link Management System 3.2
 2// This script updates the URL on each page, allowing you to share and bookmark frameset and iFrame links!
 3// Feel free to steal this code!
 4// Get help here - https://forum.melonking.net/index.php?topic=115
 5
 6// oO How to use this code Oo
 7// 1. Add '<script src="https://melonking.net/scripts/frame-link.js"></script>' to your <head>.
 8// 2. Add 'id="mainframe"' to your main iFrame or Frame window.
 9// Optional: Create a second <script></script> section AFTER you link the frame-link.js
10// Optional: add 'updateTitle = false;' if you want to disable title updating. (Default is true)
11// Optional: add 'titlePrefix = "My Site ";' if you want to add a prefix to your titles. (Default is none)
12// Optional: add 'pageParam = "z";' if you want to change the url path of your pages. (Default is z)
13// Optional: if you use a hitCounter like GoatCounter add 'hitCounterFunction = function () { XXX MY HIT COUNTER CODE }', this function will automaticly be called each time someone click a page, so you can log per page hits within your frame.
14// See https://melonking.net/melon.html for a working example! GOOD LUCK!
15
16var mainFrame;
17var firstLoad = true;
18var updateTitle = true;
19var pageParam = "z";
20var titlePrefix = "BiRabittoh | ";
21var hitCounterFunction = undefined;
22
23//Event to handle first page load - Also sets up the mainFrame
24window.addEventListener("DOMContentLoaded", (e) => {
25    mainFrame = document.getElementById("mainframe");
26    mainFrame.addEventListener("load", updateHistory, false);
27    setMainFrame();
28});
29
30//Event to handle back button presses
31window.addEventListener("popstate", function (e) {
32    if (e.state !== null) {
33        setMainFrame();
34    }
35});
36
37function goToPageInitial(page) {
38    
39}
40
41//Checks to see if a page parameter exists and sets the mainframe src to that page.
42function setMainFrame() {
43    let params = new URLSearchParams(window.location.search);
44    let page = params.get(pageParam);
45    if (page == null) return;
46
47    document.getElementById("mainframe").src = page;
48}
49
50//Updates the browser history with the current page, causing the URL bar to update.
51function updateHistory() {
52    let title = titlePrefix + mainFrame.contentDocument.title;
53
54    // Stops the page getting into an infinate loop reloading itself.
55    if (firstLoad) {
56        firstLoad = false;
57        if (updateTitle) {
58            document.title = title;
59        }
60        if (hitCounterFunction != undefined) {
61            hitCounterFunction();
62        }
63        return;
64    }
65
66    history.replaceState(null, "", "?" + pageParam + "=" + mainFrame.contentWindow.location.pathname);
67
68    if (updateTitle) {
69        document.title = title;
70    }
71
72    //Save a hit - Optionally run this if a hit counter has been defined.
73    if (hitCounterFunction != undefined) {
74        hitCounterFunction();
75    }
76}