3

Is it possible to change variable a to true without modifying the page source, so that it prints foo?

<html>

 <head>
  <script>
   var a = false;
   if(a) document.write("foo");
   else document.write("bar");
  </script>
 </head>

 <body>
 </body>

</html>

I can change variable value dinamically from Chrome. But how to reload to see "foo", not "bar"

starball
  • 20,030
  • 7
  • 43
  • 238
widehusk
  • 31
  • 3
  • 1
    without modifying the source? no. By modifying the script such that it reads a state value from `localStorage`, `sessionStorage`, cookies, AJAX, or uses a value provided by a server-side language? yes. – zzzzBov Apr 02 '15 at 13:40
  • you are asking if you can dynamically change the javascript code of a webpage that would execute on reload? that obviously would not be possible because it would mean you could hack any webpage to make it do whatever you wanted. – Rick Jun 21 '23 at 19:16

3 Answers3

0

You can use localstorage on most modern browsers see http://www.w3schools.com/html/html5_webstorage.asp

if you plan to support older browsers, you can set a cookie then read it when the document reloads. http://www.w3schools.com/js/js_cookies.asp

edit: Try this code

//function to check support for localStorage
function checkLocalStorageSupport() {
    try {
        localStorage.setItem("x", "x");
        localStorage.removeItem("x");
        return true;
    } catch (e) {
        return false;
    }
}
//function to read value of cookie
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

if (checkLocalStorageSupport()) {
    if (localStorage.getItem('a') != null)
        if (localStorage.getItem('a') == "true")  //check that the value of localStorage is not null
            document.write("foo");                  //do operation if localStorage value is true
        else
            document.write("bar");
    else
        localStorage.setItem('a', 'false');
}
else {
    if (getCookie('a') != null)                     //check that the value of cookie is not null
        if (getCookie('a') == "true")
            document.write("foo");                  //do operation if cookie value is true
        else
            document.write("bar");
    else
        document.cookie = "a=false; expires=Thu, 31 Dec 2015 12:00:00 UTC"; //if no cookie exists set a cookie
}
Rohit Agre
  • 1,528
  • 1
  • 14
  • 25
  • 1
    These are simply links to potential answers; please post some of the relevant code since links can change over time, making this answer irrelevant. – rfornal Apr 02 '15 at 13:51
0

How should this work? You cannot preserve a state on reload with a static HTML page and an embedded script. Use a cookie or some other persistent mechanism for storing the state.

Martin Krämer
  • 567
  • 3
  • 17
0

Can the value of a JS variable defined in a script be modified in that same scope upon the next reload without modifying that script?

As far as I know, no.

If you want something that can be changed between reloads but with the last stored value also persisted between page reloads, you can use cookies or localstorage.

Okay technically speaking, you can make reality whatever you want by editing things in your browser's developer tools (if the developer tools for your browser support it): put a breakpoint in a place in that variable's scope, then when the breakpoint is hit, use whatever functionality your browser's developer tools provide to edit the value of that variable. Some browsers are quite flexible with this and will even allow you to assign new values to variables declared using const. That change that you make will only last for that browsing session, and upon page reload, that change will be forgotten. But I kind of doubt this is what you're looking for. See also https://stackoverflow.com/a/73441795/11107541.

starball
  • 20,030
  • 7
  • 43
  • 238