Skip to content

Instantly share code, notes, and snippets.

@ScottKaye
Last active April 18, 2024 12:30
Show Gist options
  • Save ScottKaye/5158488 to your computer and use it in GitHub Desktop.
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…
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;
@iprutkovsky
Copy link

Works great. Big respect for useful code.

@eutinmona
Copy link

simple, smooth, brilliant, etc. the best script I've been looking for. thank you so much.

@webmicah
Copy link

Life Saver!

@luisdepau
Copy link

Works like charm.
Thanks a lot for sharing.

@rorytassell
Copy link

I don't normally comment on these (I know, I should) - but I just had to say thank you. This works brilliantly! Bravo!

@stefan-nube
Copy link

stefan-nube commented Nov 21, 2022

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! 🙏

@ScottKaye
Copy link
Author

ScottKaye commented Nov 21, 2022

@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);

@Wicher100
Copy link

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.

@Wicher100
Copy link

Wicher100 commented Feb 2, 2023

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment