简体   繁体   English

如何在Javascript中的多个浏览器中获得相同的值?

[英]How to get a value to be the same across multiple browsers in Javascript?

I'm working on a section for a web page that allows the user to see when the page was last updated.我正在处理一个网页部分,该部分允许用户查看页面上次更新的时间。

The way it's supposed to work is I choose a date in the first input box and click the update button and the second box fill the the Last Updated field with the date and the time.它应该工作的方式是我在第一个输入框中选择一个日期,然后单击更新按钮,第二个框用日期和时间填充 Last Updated 字段。

After that, I should be able to exit out of the page and when I reopen the page Last Updated field should be filled with the same date and time until I change it.之后,我应该能够退出页面,当我重新打开页面时,Last Updated 字段应该填充相同的日期和时间,直到我更改它。

I'm able to see that my code does still show the date that I have added in the last time I was on e the page, the problem I'm having is I want the result to be the same regardless of what browser I'm using it on.我能够看到我的代码仍然显示我上次在页面上添加的日期,我遇到的问题是无论我使用什么浏览器,我都希望结果相同”我使用它。

For example if the date is 2020-09-15T13:53 on Chrome I want the resulting date to be 2020-09-15T13:53 on Edge, Safari and Firefox as well.例如,如果 Chrome 上的日期是2020-09-15T13:53 ,我希望 Edge、Safari 和 Firefox 上的结果日期也是2020-09-15T13:53 Of I go on Edge and open my page there it would show nothing or another date when I edited on Edge.如果我在 Edge 上打开我的页面,当我在 Edge 上编辑时,它不会显示任何内容或其他日期。

Here's my code:这是我的代码:

    <div class="last-update-header">
        <h3> New Update Date: <input type="datetime-local" id="update" name="update-start" value="yyyy-mm-dd hh:mm"/> </h3>
        <h3> Last Updated: <input type="label; submit" id="lastUpdate"  readonly="readonly"/> </h3>
        <button id="updateButton" onclick="myFunction(); saveValue(lastUpdate)"> Update </button>
        <script>
            document.getElementById("lastUpdate").value = getSavedValue("lastUpdate");
            function myFunction(e) {
                document.getElementById("lastUpdate").value = document.getElementById("update").value;
            }
            function saveValue(e) {
                var id = e.id;
                var val = e.value;
                localStorage.setItem(id, val);
            }
            function getSavedValue(v) {
                if (!localStorage.getItem(v)) {
                    return "";
                }
                return localStorage.getItem(v);
            }
        </script>
    </div>

My question is how can I get this code where I can read the same output on all browsers and from multiple computers and not different things for different browsers?我的问题是如何获得这段代码,我可以在所有浏览器和多台计算机上读取相同的输出,而不是不同浏览器的不同内容?

You'll need to save it on the server somewhere.您需要将它保存在服务器上的某个地方。 Even if localStorage worked across different browsers (it doesn't) that would only be saved on your machine, others wouldn't be able to see it.即使 localStorage 在只保存在您的机器上的不同浏览器上工作(它不能),其他人也无法看到它。

You're probably making it unnecessarily complicated using JavaScript at all.您可能根本就使用 JavaScript 使它变得不必要地复杂。 You could just update the HTML to reflect the last updated time.您可以只更新 HTML 以反映上次更新时间。

If you really want it to work the way you've requested you'll need to use a server-side language on-top of the JavaScript but you'd also need to rewrite all of that.如果您真的希望它按照您要求的方式工作,您需要在 JavaScript 之上使用服务器端语言,但您还需要重写所有这些。

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

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