You can do this but you need a small piece of server-side work. You see in your example...
<html>
<body>
<script type="text/javascript">
if (typeof variable1 === 'undefined') {
html elements and javascript functions/code here
}
variable1 = true;
</script>
variable1
is always undefined. So everything you want to hide by using that if statement will show/load. What you can do just before that is something like this...
<html>
<body>
<script type="text/javascript">
// try using localstorage
if (!localStorage.getItem("variable1")) {
html elements and javascript functions/code here
localStorage.setItem("variable1","loaded");
}
// later on you may have a function that removes the html and javascript that was added.
function foo() {
// function stuff
localStorage.removeItem("variable1"); // remove this so content may be able to load when needed.
}
</script>