简体   繁体   English

我到底如何在函数中更改全局变量?

[英]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. 我花了3天的时间来解决这个简单的问题。 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 . 我想要的是让我的整个网站记住winversion全局变量值并能够从函数中对其进行更改。 ( YES I know global variables are more evil than darthwader but please that's the only good thing going on in my life) (是的,我知道全局变量比darthwader更邪恶,但是请这是我一生中唯一发生的事情)

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> . 我有一个带<iframe> html文件,并且当用户在内部单击时,我将来自我域的不同页面加载到该<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> ) 这是我的index.html(在<iframe>周围有一些div,但是我很确定这是无关紧要的,因为其他所有功能都可以使用,而javascript仅适用于此<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 默认的iFrame页面具有此html代码

    <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 我的script.js是这个

 // 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. 但是后来,在下一个<iframe>当我尝试在同一JavaScript文件中读取winversion时,它说未定义。 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> ). 当用户在页面1上选择Windows版本并在下一个html页面中单击链接以启动msconfig描述页面(已加载在<iframe> )时,将触发该navigatenow()函数。

    function navigatenow()
{

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

I have tried: 我努力了:

  • Using /not using var . 使用/不使用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. 使用persistjs几个小时。

  • meditating and praying! 冥想和祈祷!

NOTHING works. 没什么用。 The damned winversion variable is still as undefined as the purpose of my existence. 该死的winversion变量仍然和我存在的目的一样不确定。

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) . 我想要的只是让我的html文件记住用户选择了哪个Windows版本(不,我无法以编程方式检测到Windows版本,我的目的是询问用户版本是否不同)。

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. iframe中的页面是一个单独的窗口,它不会继承索引页面的范围。 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/ 这是一个工作示例: http : //jsfiddle.net/ga62A/

The index page loads this iframe: http://jsfiddle.net/cMDvc/1/ 索引页面会加载以下iframe: http//jsfiddle.net/cMDvc/1/
Then you go on to this igrame: http://jsfiddle.net/9qjaX/ 然后,您继续以下步骤: http : //jsfiddle.net/9qjaX/

LePrince, when you change pages, JS drops all memory of what the changes were. LePrince,当您更改页面时,JS会丢弃所有有关更改的内容。 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. JavaScript已读入页面,但您无法从最终用户的浏览器回JS文件,因为这只是在要求您的生活,网站和所有金钱被盗。

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 . 如果您需要它做更多的事情,则可以从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. 它只用于存储很少的信息,除非您编写用于获取Cookie和设置Cookie的工具,否则使用起来并不有趣。

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). 接下来是localStorage,它更加有用和更新(但旧的IE不支持它,因此装有Windows 7的XP上的人们无法使用它)。 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. Ruby(/ Rails),PHP,Node.js等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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