-
-
Save ScottKaye/5158488 to your computer and use it in GitHub Desktop.
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; |
Life Saver!
Works like charm.
Thanks a lot for sharing.
I don't normally comment on these (I know, I should) - but I just had to say thank you. This works brilliantly! Bravo!
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...
simple, smooth, brilliant, etc. the best script I've been looking for. thank you so much.