Last active
August 24, 2024 12:24
-
-
Save ScottKaye/5158488 to your computer and use it in GitHub Desktop.
Hide URL parameters using Javascript's history.replaceState methods. The server (PHP or whatever) will still see it of course, but this script will quickly 'hide' the parameters. Don't use this to hide anything sensitive - there shouldn't be anything sensitive in GET anyway, but I use this to hide various un-pretty things like ?error=invalidPass…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getURLParameter(name) { | |
return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]); | |
} | |
function hideURLParams() { | |
//Parameters to hide (ie ?success=value, ?error=value, etc) | |
var hide = ['success','error']; | |
for(var h in hide) { | |
if(getURLParameter(h)) { | |
history.replaceState(null, document.getElementsByTagName("title")[0].innerHTML, window.location.pathname); | |
} | |
} | |
} | |
//Run onload, you can do this yourself if you want to do it a different way | |
window.onload = hideURLParams; |
One downside to this is that the "state" represented by the URL parameters is lost if you reload the page - worth taking notice of
Does anyone have a solution or at least an idea worth exploring that enables us to hold the parameters even after the reload?
ps. Scott thank you so much for the solution! 🙏
@nube-duo I suppose the simplest thing to do is to store the parameters in sessionStorage and just read from there until the search object changes. This uses some newer browser APIs since I originally posted this 9 years ago, but should work:
function persistParams() {
const PARAMS_KEY = "__params";
if (performance.navigation.type === performance.navigation.TYPE_RELOAD) {
const persistedParams = sessionStorage.getItem(PARAMS_KEY);
if (persistedParams) {
return persistedParams;
}
}
sessionStorage.setItem(PARAMS_KEY, location.search);
return location.search;
}
function hideParams() {
persistParams();
history.replaceState(null, document.title, window.location.pathname);
window.addEventListener("beforeunload", () => {
history.replaceState(
null,
document.title,
window.location.pathname + (sessionStorage.getItem(PARAMS_KEY) || "")
);
});
}
window.addEventListener("load", hideParams, false);
Tnx for this!!
is there any way to adapt the script to hide the params when hovering a url somewhere on a page?
Greetz Wicher.
i made this, seems to do the trick:
<?php // testfilename: hidehoverparams.php
session_start();
?>
<!-- My piece -->
<script>
if (sessionStorage.params)
{
let urlparams = sessionStorage.getItem("params");
//clear the params
sessionStorage.removeItem("params");
// rename hidehoverparams.php to the file you want to call, this puts the params back in $_GET
window.location.href = 'https://<?php echo $_SERVER['SERVER_NAME'];?>/hidehoverparams.php?'+urlparams;
}
</script>
<!DOCTYPE HTML>
<html>
<head>
<title>hidehoverparams combined with code from @ScottKaye </title>
<!-- My piece -->
<script>
function hidehoverparams(p)
{
sessionStorage.setItem("params", p);
}
</script>
<!-- @ScottKaye's piece -->
<script>
function persistParams() {
const PARAMS_KEY = "__params";
if (performance.navigation.type === performance.navigation.TYPE_RELOAD) {
const persistedParams = sessionStorage.getItem(PARAMS_KEY);
if (persistedParams) {
return persistedParams;
}
}
sessionStorage.setItem(PARAMS_KEY, location.search);
return location.search;
}
function hideParams() {
persistParams();
history.replaceState(null, document.title, window.location.pathname);
window.addEventListener("beforeunload", () => {
history.replaceState(
null,
document.title,
window.location.pathname + (sessionStorage.getItem(PARAMS_KEY) || "")
);
});
}
window.addEventListener("load", hideParams, false);
</script>
<!-- my piece -->
</head>
<body>
<a href="hidehoverparams.php" onclick="hidehoverparams('degrees=270&lib=Fotos&catid=2&imgid=30&t=1675333259#103')">to hidehoverparams.php</a>
<?php
if(isset($_GET['degrees']))
{
print_r($_GET);
$_GET = array();
}
?>
</body>
</html>
thank you, its very usefull...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't normally comment on these (I know, I should) - but I just had to say thank you. This works brilliantly! Bravo!