<html> <head> <style> /* Make all links look active even when href isn't set. We do this so we can set the onclick attribute instead. */ a { color: #0000EE; text-decoration: underline; cursor: pointer; } a:visited { color: #551A8B; text-decoration: underline; cursor: pointer; } </style> </head> <body> <div id="content"> </div> <script> /* Lukas quickly coded the stuff below. Use at your own risk.... // // This script fakes links to local webpages by replacing instances of href="/pageName.html" with onclick="load(pageName);" // The load(pageName) function replaces content from one webpage with content from another. // // OG pages copied from Course Builder // Example: { 'index.html': '<h1>Hello, World!</h1>' } */ let pageBodies = { 'index.html': '<h5>Investfriend</h5> <p>Learn all about stocks and bonds below:</p> <a href="/stocks.html">Stocks</a> <a href="/bonds.html">Bonds</a>',
'stocks.html': '<a href="/index.html">Back</a> <h5>Stocks</h5> <p>The difference between stocks and bonds is that stocks are shares in the ownership of a business, while bonds are a form of debt that the issuing entity promises to repay at some point in the future. This means that stocks are a riskier investment than bonds.</p> <p>Subscribe to find out more!</p>',
'bonds.html': '<a href="/index.html">Back</a> <h5>Bonds</h5> <p>The difference between stocks and bonds is that stocks are shares in the ownership of a business, while bonds are a form of debt that the issuing entity promises to repay at some point in the future. This means that stocks are a riskier investment than bonds.</p> <p>Subscribe to find out more!</p>', }; /* // Create a pathToOnClickMapping object where keys are href absolute paths and values are onclick attributes replacing the page's body // Example: { 'href="/trail.html"': 'load(\'trail.html\');' } */ let pathToOnClickMapping = {}; Object.keys(pageBodies).forEach((key) => { pathToOnClickMapping['href="/' + key + '"'] = 'onclick="load(\'' + key + '\');"'; }); let regex = new RegExp(Object.keys(pathToOnClickMapping).join("|"), "gi"); /* // Create a pagesWithAbsolutePathsReplaced object that stores webpage bodies with absolute paths replaced with onclick=load(pageName) */ let pagesWithAbsolutePathsReplaced = {}; Object.keys(pageBodies).forEach((key) => { pagesWithAbsolutePathsReplaced[key] = pageBodies[key].replace(regex, function(matched) { return pathToOnClickMapping[matched]; }); }); /* // Replace content of webpage with page from pagesWithAbsolutePathsReplaced */ function load(pageName) { document.getElementById('content').innerHTML = pagesWithAbsolutePathsReplaced[pageName]; }; load('index.html'); </script> </body> </html>]]>