简体   繁体   中英

Refresh/Reload web page only once

<script type="text/javascript">
    $(document).ready(function () {

        //Check if the current URL contains '#' 
        if (document.URL.indexOf("#") == -1) {
            alert('dfjdkjfkdj');
            // Set the URL to whatever it was plus "#".
            url = document.URL + "#";
            location = "#";

            //Reload the page
            location.reload(true);

        }
    });
</script>

Above code is not working. What is the problem?

Setting document.URL doesn't change the url in the browser address bar. Use location.href instead:

location.href = location.href + '#';

On Page load the document.ready will execute and than it will apped the URL with # without any refresh/ reload. Here is the code for this.

$(document).ready(function () {

        //Check if the current URL contains '#' 
        if (document.URL.indexOf("#") == -1) {
            alert('dfjdkjfkdj');
            // Set the URL to whatever it was plus "#".
            window.location.href = "#";

            //Reload the page
            window.location.href = window.location.href;

        }
    });

Hope this helps.

You need to try this. This code should work fine.

<script type="text/javascript">
    $(document).ready(function () {

        //Check if the current URL contains '#' 
        if (document.URL.indexOf("#") == -1) {
            alert('dfjdkjfkdj');
            // Set the URL to whatever it was plus "#".
            window.location.href = window.location.href + "#";

            //Reload the page
            window.location.reload(true);

        }
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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