简体   繁体   English

Windows / Tab之间的javascript / jquery内存空间

[英]javascript / jquery memory space between windows / tabs

I have a page that opens a popup with contents like below: 我有一个页面,打开一个带有如下内容的弹出窗口:

<html>
    <head>

    <script>
    var fileValidationRules = {};
    fileValidationRules.cms_file_input = { exp: "\.(jpg|gif|xls|doc|ppt|pdf|zip)$", desc: "valid download file type", maxKb: 1048576, maxKbDesc: "1 Gb" };
    </script>

    <script src="upload.js?"></script>
    </head>

    <body>

        <form>
            <input type='hidden' id='cms_file_fragment_id' value='303'>

            <button id='cms_file_input_submit_button' type='button' class='btn btn-primary btn-block'> Upload file </button>

        </form>

    </body>
</html>

The list of file types and the hidden form field value are different each time the popup is opened, depending in the link clicked. 每次打开弹出窗口时,文件类型列表和隐藏的表单字段值都不同,具体取决于单击的链接。

Suppose the user opens two popups (it seems the browser does not allow that in one tab, but will open two popups from different main window tabs). 假设用户打开了两个弹出窗口(似乎浏览器不允许在一个选项卡中打开该弹出窗口,但是将从不同的主窗口选项卡中打开两个弹出窗口)。 In file.js I'll get use fileValidationRules and do: file.js我会使用fileValidationRules做:

var id = $("#cms_file_fragment_id").val();

Question is - do I need to take any precautions to ensure I'm using variable values from the correct window? 问题是-我需要采取任何预防措施来确保我使用的是正确窗口中的变量值吗?

Question is - do I need to take any precautions to ensure I'm using variable values from the correct window? 问题是-我需要采取任何预防措施来确保我使用的是正确窗口中的变量值吗?

No. Each window gets its own global environment, which is entirely distinct from other global environments. 不。每个窗口都有自己的全局环境,这与其他全局环境完全不同。

If there's a relationship between the windows (parent/child, opener/opened, etc.), it's sometimes possible to access globals of one window in another explicitly via (for instance) parent.nameOfGlobalHere . 如果窗口之间存在关联(父/子,打开/打开等),则有时可以(例如)通过parent.nameOfGlobalHere 显式访问另一个窗口的全局变量。 But that's explicit, and only works for globals that are properties of the global object. 但这是很明显的,并且仅适用于作为全局对象属性的全局变量。 (They all used to be, but as of ES2015, some aren't.) (它们以前都是,但是从ES2015开始,有些不是。)

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

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