如何在调用函数之前重定向?

时间:2013-12-10 17:43:30

标签: javascript redirect cookies

我创建了一个网站,通过创建一个带有+1计数器的cookie来计算用户访问过的次数。每五次访问后,它会要求用户注册。一旦用户注册,它就会删除cookie并将其替换为注册信息(它适用于学校,或者我永远不会这样做)。创建新cookie后,它会重定向到一个新页面,该页面显示来自cookie的信息。然后,如果用户再次访问该站点,则应该使用与第一次重定向相同的条件在计数器启动之前重定向。那不行。它无论如何都会创建计数器cookie而无需重定向。

我使用的代码是:

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title>

        </title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <script type="text/javascript">

/////// Variable "name" is what gives the cookie its name. ///////
    var name = "cookie";
        function redirect() {
var x = document.forms.reg.first.value;
var y = document.forms.reg.last.value;  
        if (document.cookie == "Name: " + x + " Email: " + y) {
            window.location="registered.html";
            }
            else {
                setCookie();
            }
        }
/////// This function will set the cookie if no cookie exists. ///////
//////// Then it will add the value of 1 to the cookie name. ////////   
    function setCookie() {

        if (document.cookie) {
            i = document.cookie.indexOf(name);
        }       
        else {
            i = -1;
        }
        if (i == -1) {
            document.cookie = name+ "=1; max-age=" +2592000;
        }    
        else {
            var start = (document.cookie.indexOf("=", i) + 1);
            var end = document.cookie.indexOf(";", i); 
            if (end == -1) {
                end = document.cookie.length;
            }
            var count = eval(document.cookie.substring(start, end)) + 1;
            document.cookie = name+ "=" +count+ "; max-age=" + 2592000;
        var popup = count % 5;
        if (popup == 0) {
            alert("Please Register");
        }
        else {
            return
        }   
        }
        }

    function formCheck() {
    var x = document.forms.reg.first.value
    var y = document.forms.reg.last.value
        if (x =="") {
            alert("Something");
        return false;
        }
        if (y =="") {
            alert("Something Else");
        return false;   
        }
        else {
            document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
            document.cookie = "Name: " + x + " Email: " + y;
            alert(document.cookie);
        }
        if (document.cookie == "Name: " + x + " Email: " + y){
            window.location.replace("registered.html");
            return false;
        }
        }

        </script>       
    </head>
<body onload="redirect();">

<form name="reg" onSubmit="return(formCheck());">
Type your first name: <input type="text" name="first">
    <br>
Type your last name: <input type="text" name="last">
<input type="submit">
</form>

</body>
</html>
</html>

任何帮助将不胜感激。从我提出的最后一个问题开始,我已经走了很长一段路,而且我几乎为自己感到骄傲,但这是踢我的屁股。到目前为止我花了将近20个小时,这是我需要的最后一件事。

谢谢。

UPDATE!

我能够使用这项工作来实现redirect()

function redirect() {
    if (document.cookie.length > 15) {
        window.location.replace("registered.html");
    }
    else {
        setCookie();
    }
    }

1 个答案:

答案 0 :(得分:0)

我在执行函数调用时遇到问题所以我从头开始,我相信这可以满足您的需求。请查看它并相应地修改您的代码,而不是将其用作最终代码。

<html>
<head>
    <title>Test</title>
    <script>            
        function updateVisits(){
            var num_visits = getCookie('visits');
            if( !num_visits ) {
                num_visits = 0;                   // first visit
            }
            num_visits++;                         // add 1
            setCookie ('visits', num_visits, 30); // create/update cookie
        }

        function checkVisits(){
            var nag_limit = 5; // nag on 5+ visits
            if(parseInt(getCookie('visits')) >= nag_limit) {
                alert('Please register');
            }
        }

        // Does user cookie exist?
        function isRegistered(){
            if(getCookie('user')) {
                return true;
            } else {
                return false;
            }    
        }

        function registerUser()
        {
            var first = document.forms.reg.first.value;
            var last = document.forms.reg.last.value;

            if(first == ''){
                alert('Please enter first name');
            } else if(last == '') {
                alert('Please enter last name');
            } else { // everything checked out
                var cookie_value = first + ' ' + last;
                setCookie('user', cookie_value, 30); // save user cookie
                window.location = 'registered.html';
            }
            return false;
        }

        function setCookie(c_name,value,exdays)
        {
            var exdate=new Date();
            exdate.setDate(exdate.getDate() + exdays);
            var c_value=escape(value) +
                    ((exdays==null) ? "" : ("; expires="+exdate.toUTCString()));
            document.cookie=c_name + "=" + c_value;
        }

        function getCookie(c_name)
        {
            var i,x,y,ARRcookies=document.cookie.split(";");
            for (i=0;i<ARRcookies.length;i++) {
                x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
                y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
                x=x.replace(/^\s+|\s+$/g,"");
                if (x==c_name) {
                    return unescape(y);
                }
            }
        }

        // Run every page load
        window.onload = function(){
            var registered = isRegistered();  // check if already registered, then redirect
            if(registered) {
                window.location = 'registered.html'; // user already registered
            } else {
                updateVisits();     // set/update cookie
                checkVisits();      // nag user if needed
            }
        };
    </script>
</head>
<body>

<form name="reg" onSubmit="return(registerUser());">
    First name: <input type="text" name="first"><br>
    Last name: <input type="text" name="last">
    <input type="submit">
</form>

</body>
</html>