Reload the same URL with a different hash (fragment)

3

In response to an AJAX call:

$.ajax({
          type: "POST",
          dataType: 'json',
          data: {
                  'descripcion': descripcion
                },
          url: "<?php echo site_url();?>"+"admin",
          success : function(data) {
            console.log(data);
            if (data == "1") {
              swal(
                    'Correcto!',
                    'Grupo agregado correctamente!',
                    'success'
                  ).then(function () {
                    $('#add').modal('close');
                    document.location.href = "test#tab1";
                  })
            }else if (data == "2") {
              swal(
                    'Error!',
                    'Ha ocurrido un error, contacte al administrador!',
                    'error'
                  )
            }else if (data == "0") {
              swal(
                    'Error!',
                    'Todos los campos son obligatorios!',
                    'error'
                  )
            }
          }
      });

I want to reload the page (the same URL), but with a different # (hash or fragment). The problem I have is on the line:

document.location.href = "test#tab1";

Since it does not work, that is, it does not reload the page in the #tab1 section.

If I remove #tab1 it works normal, but it is at the top of the page and does not go to that element.

How could I solve this?

    
asked by Juan Pinzón 24.01.2018 в 15:17
source

1 answer

5

The browser realizes that it is the same page, so do not try to reload it, just look for the #tab1 within the current page.

To refresh it, you have to change the #tab1 and force it with location.reload(true)

location.hash = 'tab1';
location.reload(true);
    
answered by 24.01.2018 / 16:08
source