Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

javascript - 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.

This all works fine in Firefox however doesn't work in Edge or 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>
question from:https://stackoverflow.com/questions/65883964/js-for-setting-cookies-on-form-submit-works-in-firefox-but-not-edge-chrome

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do you test the code through file protocol like 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. You need to run your page on a http/https server then the code can work on all browsers.

Result: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...