0
<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?

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
Abhishek Gupta
  • 109
  • 1
  • 7

3 Answers3

0

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

location.href = location.href + '#';
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

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.

Vivek Tankaria
  • 1,301
  • 2
  • 15
  • 35
0

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>
Geeky Ninja
  • 6,002
  • 8
  • 41
  • 54