You could take the code and make it a IIFE (Immediately Invoked Function Expression) and put it in a single file like (route.js) just add the external link to it on the pages you want to apply the logic to.
route.js
file contents
(function() {
const queryStrParts = window.location.search.substr(1).split("&");
const queryStrLookup = queryStrParts.reduce((lookupObj, part) => {
const [ param, value ] = part.split("=");
return {...lookupObj, [decodeURIComponent(param)]: decodeURIComponent(value)}
}, {});
const paramD = queryStrLookup.d;
const redirectPath = paramD === "9"
? './menu.html'
: './logout.html'
window.location.href = redirectPath;
})();
In your HTML file:
<head>
<title>test</title>
<script src="./route.js"></script>
</head>
Then you would need to add that logic to the code.
Thank you, @RandellDawson
This is over my head for now.
It appears that if there is nothing after the .html (no ?variable=value), the split does not progress and the page calling route.js is opened anyway.
I guess that for now I’ll just use my index.html >>> test >>> (good test) >>> menu in a hidden directory. I just don’t have the time now to try and understand JavaScript.
Ideally, I’d have to pass the variable name and expected value to the function from the calling page, then, only if the test fails, redirect to logout.
– Fred
I am not sure what you mean. Can you walk me through an example of what is happening?
@RandellDawson , consider ./two/three.html below.
If the link ( Three ) from my menu is used, the page opens.
If you enter the url of “./two/three.html” in the address bar, you will be sent to “…/one/logout.html” This is what I want.
Repeat the test for page …/two/two.html which calls “…/one/route.js”, the page will open! With no variable and value the script does not redirect to logout.htm as I wish.
<!DOCTYPE html> <!-- ./one/menu.html -->
<html lang="en-US">
<head>
<title>Menu</title>
<script>
var parts = window.location.search.substr(1).split("&");
var $_GET = {};
for (var i = 0; i < parts.length; i++) {
var temp = parts[i].split("=");
$_GET[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
}
if (parts[3] !== "d=9") {
window.location = "../one/logout.html";
}
</script>
</head>
<body>
<h1>Menu</h1>
<a href="../two/two.html?a=0&b=7&c=2&d=9&e=4&f=1&g=M&h=6&i=5">Two</a>
<br>
<a href="../two/three.html?a=0&b=7&c=2&d=9&e=4&f=1&g=M&h=6&i=5">Three</a>
</body>
</html>
=================================================================
<!DOCTYPE html> <!-- ./two/two.html Did the viewer come in the front door? -->
<html lang="en-US">
<head>
<title>Two</title>
<script src="../one/route.js"></script>
</head>
<body>
<h1>Two</h1>
</body>
</html>
================================================================
<!DOCTYPE html> <!-- ./two/three.html Did the viewer come in the front door? -->
<html lang="en-US">
<head>
<title>Three</title>
<script>
var parts = window.location.search.substr(1).split("&");
var $_GET = {};
for (var i = 0; i < parts.length; i++) {
var temp = parts[i].split("=");
$_GET[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
}
if (parts[3] !== "d=9") {
window.location = "../one/logout.html";
}
</script>
</head>
<body>
<h1>Three</h1>
</body>
</html>
===============================================================
and ../one/route.js is below:
===============================================================
(function() {
const queryStrParts = window.location.search.substr(1).split("&");
const queryStrLookup = queryStrParts.reduce((lookupObj, part) => {
const [ param, value ] = part.split("=");
return {...lookupObj, [decodeURIComponent(param)]: decodeURIComponent(value)}
}, {});
const paramD = queryStrLookup.d;
const redirectPath = paramD !== "9"
? './logout.html'
:
window.location.href = redirectPath;
})();
===============================================================
Thanks again,
-- Fred