简体   繁体   中英

How on earth do I change a global variable from within a function?

I have spent 3 days trying to figure this SIMPLE problem out. All I want is for my whole website to remember the winversion global variable value and to be able to change it from within a function . ( YES I know global variables are more evil than darthwader but please that's the only good thing going on in my life)

I have one html file with an <iframe> , and as the user clicks inside, I load up different pages ( from my domain) into that <iframe> .

This is my index.html ( it has a few divs around the <iframe> but I'm pretty sure that's irrelevant because everything else works, and javascript is ONLY for this <iframe> )

 <script type="text/javascript" src='script.js'></script>
---------- ( some inner body html , not important)----------------

 <iframe id="myiframe" src="mainproblems.html" >

The default iFrame page has this html code

    <a href="#" onclick="setwinver('xp')";>I have  Windows XP </a>
    <a href="#" onclick="setwinver('win7')";> have Windows 7/Vista </a>

My script.js is this

 // This is supposed to be my global variable to hold windows version value
 var winversion ; 

    // This function (I officially hate it now) is supposed to change 
    // winversion variable based on what user clicked on the iFrame
    function setwinver(ver) 

{   
        //so now winversion should either have xp or win7 assigned
        winversion=ver; 
}

But later on, in the next <iframe> when I try to READ winversion in the same JavaScript file, it says undefined. This navigatenow() function is triggered when the user has selected windows version on page 1 and in the next html page click a link to launch msconfig description page( which is loaded in the <iframe> ).

    function navigatenow()
{

        if (winversion == "xp")
    {
         window.location.href=("msconfigxp.html");
    }
    else
    {
        window.location.href=("msconfig7.html");
    }
}

I have tried:

  • Using /not using var .

  • using /notusing single quotes,double quotes( though It makes no difference I know).

  • reading up on a dozen articles on hoisting.

  • using persistjs for several hours.

  • meditating and praying!

NOTHING works. The damned winversion variable is still as undefined as the purpose of my existence.

All I want is for my html file to remember which windows version user chose( no I cannot detect the windows version programmatically, my purpose of asking the user the version is different) .

Any help would be greatly appreciated fixing this and if not, please suggest a suitable cliff where I can jump off from.

The page in the iframe is a separate window, it won't inherit the scope of the index page. You need to specify that you want to go to the parent window and use the script there:

window.parent.setwinver('xp');

And the same accessing the variable:

if (window.parent.winversion == "xp")

Here is a working example: http://jsfiddle.net/ga62A/

The index page loads this iframe: http://jsfiddle.net/cMDvc/1/
Then you go on to this igrame: http://jsfiddle.net/9qjaX/

LePrince, when you change pages, JS drops all memory of what the changes were. JavaScript is read into the page, but you can not write back to the JS file, from the end-user's browser, because that would just be asking to have your life, your site and all of your money stolen.

All of the changes you make to the original code you read are stored in memory. That memory only lasts as long as the page lasts. After that, it reloads the page again, the same as it always was.

If you need it to do more than this, the first place you can start is with document.cookie . It's meant for storing very small bits of information, and it isn't fun to use, unless you write tools to get cookies and set cookies.

Next is localStorage, which is more useful and newer (but old IE doesn't support it, so people on XP with Windows 7 can't use it). But this is still only good for data for one user, and will not change anything for any other user.

If you want the ability to add... ...say, comments, or star-ratings, where other people can see posts and ratings and uploaded images, then you need to look into server-side programming. Ruby(/Rails), PHP, Node.js and the like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM