简体   繁体   English

用于在表单提交上设置 cookies 的 JS 适用于 Firefox 但不适用于 Edge/Chrome

[英]JS for setting cookies on Form Submit works in Firefox but not Edge/Chrome

I have a bootstrap modal that pops up when a page loads asking a user to set their location and saves this value to a cookie.我有一个引导模式,当页面加载要求用户设置他们的位置并将这个值保存到 cookie 时会弹出。

This all works fine in Firefox however doesn't work in Edge or Chrome.这在 Firefox 中一切正常,但在 Edge 或 Chrome 中不起作用。 Anyone have any ideas?有人有想法么?

<script>
function setCookie(cookieName, cookieValue, nDays) {
    
    var today = new Date();
    var expire = new Date();

    if (!nDays) 
        nDays=1;
    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
    return false
}
</script>

<div class="modal_container">
     
        <!-- Modal -->
        <div class="modal fade" id="store_prompt" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
              <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title" id="exampleModalLongTitle">Location Number</h5>
                </div>
                <div class="modal-body">
                    <form onsubmit="javascript:return setCookie('location', this.querySelector('select').value, 365);">
                        <div class="form-group">
                            <label for="LocationSelect">Please select your location below.</label>
                            <select class="form-control" id="LocationSelect">
                              <option value='1' >1 - Location A</option>
                              <option value='2' >2 - Location B</option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary btn-lg btn-block" onclick="closemodal(); location.reload();">Save changes</button>
                    </form> 
                </div>
              </div>
            </div>
</div>

Do you test the code through file protocol like file:///C:/... ?您是否通过文件协议(如file:///C:/...测试代码? If so, I can reproduce the situation you encountered.如果是这样,我可以重现您遇到的情况。

Your code is right.你的代码是对的。 But cookies are not set when browsing using the file:/// protocol, depending on the browser.但是使用file:///协议浏览时没有设置 cookies,具体取决于浏览器。 You need to run your page on a http/https server then the code can work on all browsers.您需要在http/https服务器上运行您的页面,然后代码才能在所有浏览器上运行。

Result:结果: 在此处输入图像描述

I think this might be because of the issues mentioned in this question.我认为这可能是因为这个问题中提到的问题。 Please go through these.请通过这些 go。 It might solve your issue.它可能会解决您的问题。 Why would setting document.cookie not work in Chrome? 为什么设置 document.cookie 在 Chrome 中不起作用?

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

相关问题 js 代码适用于 firefox 但不适用于 chrome 歌剧和边缘 - a js code works on firefox but not on chrome opera and edge JS 代码适用于 Firefox 但不适用于 Chrome 或 Edge - JS code works on Firefox but not on Chrome or Edge Chrome上的Jquery Form.submit()可以运行但不适用于Firefox - Jquery Form.submit() on Chrome works but not in Firefox Angular 2/4:nativeElement.submit()在Firefox中不提交表单,但在chrome中工作 - Angular 2/4 : nativeElement.submit() does not submit form in Firefox, but works in chrome JavaScript表单验证适用于Chrome / Firefox,而不适用于IE / Edge - JavaScript Form Validation works in Chrome/Firefox not IE/Edge 登录可以在Chrome和Firefox上使用,但不能在Edge上使用 - Login works on Chrome and Firefox but not on Edge jquery 适用于 Firefox、Edge,但不适用于 Chrome - jquery works in Firefox, Edge, but not Chrome ScrollTop可在Chrome和Edge中使用,但不能在Firefox中使用 - ScrollTop works in Chrome and Edge but not Firefox 表单提交按钮在 Firefox 中不起作用,但在 Chrome 或 IE 中有效 - Form submit button does not work in Firefox but works in Chrome or IE 捕获提交适用于Firefox但不适用于Chrome - Catching the Submit works in Firefox but not in Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM