Java script looks for STRING in HEADER HTML

0

Good morning, friends! I have a question regarding an operation that I need to perform after sending the data of a form. I have a form that its proper fields to fill in and a function to "submit" this typed information: My question is this: after submitting this information, a page will be loaded, I need a js (or any other code that meets) that reads the Header of this new page and look for the following value: "Cookie = 1" If it finds this value, then I want the user to be directed to another URL.

Any ideas?

ps: The code below is the form submission, after the execution of this I want "something" to read the header for the string I mentioned above.

submitInputValue: "Enviar",
        setFields: function(e) {
            switch (e) {
                case "login":
                    this.loginFields = !0, this.submitInputValue = "Acessar";
                    break;
                case "pwdWillExp":
                    this.pwdWillExpFields = !0, this.submitInputValue = "Alterar a Senha";
                    break;
                case "changePwd":
                    this.changePwdFields = !0, this.submitInputValue = "Alterar a Senha";
                    break;
                case "pwdNotChanged":
                    this.pwdNotChangedFields = !0, this.submitInputValue = "Alterar a Senha";
                    break;
                case "sessionExpired":
                    this.sessionExpiredFields = !0, this.submitInputValue = "Acessar";
                    break;
                case "acessPrivate":
                    this.accessPrivateFields = !0, this.submitInputValue = "Acessar";
                    break;
                case "solicitPwdChg":
                    this.solicitPwdChgFields = !0, this.submitInputValue = "Alterar a Senha";
                    break;
                default:
                    this.loginFields = !0, this.submitInputValue = "Acessar"
            }
    
asked by anonymous 21.03.2016 / 14:07

1 answer

0

You should use PHP or JS functions that work with Cookies.

Basically you need to Write and Read a Cookie, so see the example below and apply your need.

Example with PHP:

To Write a Cookie do:

<?php

//O ultimo parâmetro é o tempo que o Cookie deve ficar no navegador.
setcookie("name_do_cookie", "value", time()+60*60);

?>

To read the information stored in Cookie do:

<?php
echo $_COOKIE["name_do_cookie"];
?>

For just upgrade do:

<?php
setcookie("name_do_cookie","novo_valor");
?>

To delete do:

<?php
unset($_COOKIE["name_do_cookie"]);
/* ou */
setcookie("name_do_cookie","",time()-1);
?>

In JS you can use the functions below:

To create:

<script>
//Criar
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
} 

//Para ler
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
} 

function checkCookie() {
    var username=getCookie("username");
    if (username!="") {
        alert("Welcome again " + username);
    }else{
        username = prompt("Please enter your name:", "");
        if (username != "" && username != null) {
            setCookie("username", username, 365);
        }
    }
} 
</script>

Reference: link

link

    
21.03.2016 / 17:22