【问题标题】:How do I redirect before function can be called?在调用函数之前如何重定向?
【发布时间】:2013-12-10 17:43:29
【问题描述】:

我创建了一个网站,该网站通过创建一个带有 +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 个小时才走到这一步,而这是我最不需要的。

谢谢。

更新!

我能够使用重定向()的这项工作来使其工作

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

【问题讨论】:

  • 因此,从我能够收集到的原因来看,重定向无法在访问中起作用,是因为它依赖于具有 x,y 值的 cookie,而这些值不再存在。那么,如果它们不再具有 document.form...." 的值,那么如果存在新的 cookie,那么另一种在访问时重定向的方法是什么?
  • 您在registered.html上的代码是什么?
  • 只是一个绑定到 body onload 的函数,它写出 cookie 的值。

标签: javascript redirect cookies


【解决方案1】:

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

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

【讨论】:

  • 非常感谢。我已经用我所拥有的东西上交了我的任务,但我认为这至少会派上用场,至少看看我可以在哪里清理东西,或者让它们运行得更顺畅。不过,我很难理解人们的代码,因为我对 JavaScript 还很陌生。因此,我需要一段时间,并在谷歌上搜索一下才能完全理解每件事的作用。再次感谢您。
猜你喜欢
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多