Doubt about window.location.href()

Asked

Viewed 1,203 times

-1

I know that window.open() will be open to url in a new window and that the window.location.href() will open on the calling page. It happens that when I use the window.location.href(), I put in page_load a javascript call(alert()) by code behind and did not fire. However, this page(call) generates a excel and this is happening, working perfectly. I put a loading on it(in apsx) and it didn’t shoot either, because the page looks like it runs in the background, I don’t know if that’s true, but that’s what it looks like, but still alerts should be fired or not?


This is the function that calls the page that loads the spreadsheet(Note the various attempts on I made and none worked.

function AcaoAvancar() {

            if (ValidaFormulario()) {

                var dataInicial = $("#txt_dt_ref_inicial").val();
                var dataFinal = $("#txt_dt_ref_final").val();
                var tipoTabela = $('#ddl_tipotabela option:selected').val();
                var tabelas = RetornaTabelas();
                var classificacoes = RetornaClassificacoes();
                var grupos = RetornaGrupos();
                var autorizacao = $('#ddl_autorizacaoprevia option:selected').val();
                var formato = $("input[name='formato']:checked").val();

                var strOpcao = "dtinicial=" + dataInicial;
                strOpcao = strOpcao + "&dtfinal=" + dataFinal;
                if (tipoTabela != "") { strOpcao = strOpcao + "&tipotab=" + tipoTabela; }
                if (tabelas != "") { strOpcao = strOpcao + "&tabl=" + tabelas; }
                if (classificacoes != "") { strOpcao = strOpcao + "&class=" + classificacoes; }
                if (grupos != "") { strOpcao = strOpcao + "&grp=" + grupos; }
                if (autorizacao != "") { strOpcao = strOpcao + "&aut=" + autorizacao; }
                if (formato != "") { strOpcao = strOpcao + "&format=" + formato; }

                if (formato == "PDF")
                    window.location.href = "../../../hes/asp/hes1015b.asp?" + strOpcao;
                else {

                    window.location.href = '../../relatorios/Rel_ItensMaterialMedicamentoExcel.aspx?' + strOpcao;
                    //window.open('../../relatorios/Rel_ItensMaterialMedicamentoExcel.aspx?' + strOpcao);
                    //document.location.href = '../../relatorios/Rel_ItensMaterialMedicamentoExcel.aspx?' + strOpcao;
                    //$("#carregaLoad").load();
                }
            }
        }

This is the Page_load where the database data is processed into the spreadsheet. This code is on the page called and it is this that is my problem. You see, I put some calls in to some Alerts and none of them were fired:

protected void Page_Load(object sender, EventArgs e)
        {           
            try
            {

                Page.ClientScript.RegisterStartupScript(this.GetType(), "mensagem", "Mensagem()", true);

                //Parametros do filtro
                string dataInicial = Request.QueryString["dtinicial"];
                string dataFinal = Request.QueryString["dtfinal"];
                string tipoTabela = Request.QueryString["tipotab"];
                string tabela = Request.QueryString["tabl"];
                string classificacao = Request.QueryString["class"];
                string grupo = Request.QueryString["grp"];
                string autorizacao = Request.QueryString["aut"];

                //Carregar Lista de objetos RelatorioItensMatMed
                List<RelatorioItensMatMed> listaItensMatMed = (new Rel_ItensMaterialMedicamento_BS()).get_Rel_ItensMatMed_BS(dataInicial, dataFinal, tipoTabela, tabela, classificacao, grupo, autorizacao);

                //Preencher os dados do arquivo csv
                StringBuilder cabecalho = new StringBuilder();
                cabecalho.Append("Data de Inclusão;");
                cabecalho.Append("Tipo de Tabela;");
                cabecalho.Append("Tabela;");
                cabecalho.Append("Codigo;");
                cabecalho.Append("TUSS;");
                cabecalho.Append("Descrição;");
                cabecalho.Append("Fabricante;");
                cabecalho.Append("Referência do Fabricante;");
                cabecalho.Append("Registro ANVISA;");
                cabecalho.Append("Classificação SIMPRO;");
                cabecalho.Append("Grupo Mat/Med;");
                cabecalho.Append("Grupo Estatístico;");
                cabecalho.Append("Autorização Prévia;");
                cabecalho.Append("Última Vigência;");
                cabecalho.Append("Valor;");
                cabecalho.Append("Prestador Tabela Própria;");

                Response.Write(cabecalho.ToString());
                Response.Write("\r");

                foreach (var item in listaItensMatMed)
                {
                    StringBuilder itens = new StringBuilder();
                    itens.Append(item.DataInclusao + ";");
                    itens.Append(item.TipoTabela + ";");
                    itens.Append(item.Tabela + ";");
                    itens.Append("\t" + item.Codigo + ";");
                    itens.Append(item.TUSS + ";");
                    itens.Append(item.Descricao + ";");
                    itens.Append(item.Fabricante + ";");
                    itens.Append(item.ReferenciaFabricante + ";");
                    itens.Append(item.RegistroAnvisa + ";");
                    itens.Append(item.ClassificacaoSimpro + ";");
                    itens.Append(item.GrupoMatMed + ";");
                    itens.Append(item.GrupoEstatistico + ";");
                    itens.Append(item.AutorizacaoPrevisa + ";");
                    itens.Append(item.UltimaVigencia + ";");
                    itens.Append("\t" + item.Valor + ";");
                    itens.Append(item.PrestadorTabelaPropria + ";");

                    Response.Write(itens.ToString());

                    Response.Write("\r");
                    //Response.Flush();
                }

                Page.ClientScript.RegisterStartupScript(this.GetType(), "mensagem", "Final()", true);

                Response.ContentType = "text/plain";
                Response.AppendHeader("Content-Disposition", "attachment; filename=relacao_materiais_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".csv");
                Response.End();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

1 answer

0

Hello, I’m not sure I quite understand your question.

But when loading a new page, the current document is closed and starts loading a new document. Any code in your current document will no longer be active when the new page starts loading.

To be able to fire Alert when the new page loads, you will need to enter the code into the new page document or else load this page into an iframe and see when it is loaded into the current document so you can fire Alert.

  • Man, here’s the thing. All this is done. From what I read about Location, the calling page is not on the back, as you said, but rather the calling page. I made the call by code Behind, but I can test by js the moment the gift is mounted. I placed the call in Pageload. This did not work, but the spreadsheet is filled and everything in Pageload.

  • It is that the way you seem to need, would need a callback to be executed after running Location, however this is not possible, I believe that by js will solve the problem...

  • And how would I do that? Do you have any idea what I do? A light please, that’s on the bone.

  • You just need to fire Alert when the page that generates excel is opened, right?

  • In reality, what I need is that while the database data is loaded, I need to fire a loading. If the loading is on the calling page (it is visible) when the page is called, it stops while the calling page is processed. If I put on the page called (I believe it is correct) it is not shown. When everything ends, here comes the download of the spreadsheet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.