简体   繁体   English

将用户从输入重定向到 URL,存储在 cookie 中

[英]Redirect a user to a URL from input, store in cookie

I'm working on a project in which the user is sent to the directory name they entered in an input, like so:我正在处理一个项目,其中用户被发送到他们在输入中输入的目录名称,如下所示:

function sendanswer(e) {
if (e.keyCode === 13) {
    e.preventDefault();
    var answer = document.answerarea.input.value;
    if (answer) {window.location.href = answer;}
}
}

document.answerarea.input.onkeypress = sendanswer;

This works fine.这工作正常。 But now I want for the user to be automatically redirected to the directory they specified every time they visit the page, BUT only if they didn't recieve a 404 error after navigating to the directory.但是现在我希望用户在每次访问页面时自动重定向到他们指定的目录,但前提是他们在导航到目录后没有收到 404 错误。 I imagine this would be accomplished by erasing the cookie when the 404 page is visited.我想这可以通过在访问 404 页面时擦除 cookie 来实现。

But how would the redirecting-to-cookie process work?但是重定向到 cookie 的过程是如何工作的呢?

When you get input from use set a cookie using this code:当您从 use 获得输入时,请使用以下代码设置 cookie:

function sendanswer(e) {
  if (e.keyCode === 13) {
      e.preventDefault();
      var answer = document.answerarea.input.value;
      if (answer) {
         window.location.href = answer;
         //SET COOKIE WITH NAME redirectPath
         document.cookie="redirectPath="+ answer;
      }
  }
}

Now on your home page (the page which user gets on visiting your site) add following call on-page load:现在在您的主页(用户访问您网站的页面)上添加以下调用页面加载:

window.onload=function(){
    var kuki = "redirectPath=";//NAME OF COOKIE WE SET
    var cookies = document.cookie.split(';');
    for(var i=0;i < cookies.length;i++) {
        var c = cookies[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(kuki) == 0){
           var path = c.substring(kuki.length,c.length);
           //MOVE USER TO STORED PATH
           document.location.href=path;
        }
    }   

} }

A better approach will be to read cookie on server-side and redirect user to their favourite folder from there.更好的方法是在服务器端读取 cookie 并将用户从那里重定向到他们最喜欢的文件夹。

For more reference check this answer.有关更多参考,请查看答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户输入的javascript cookie - javascript cookie from user input 如何将当前的 URL 存储在 cookie 中,并使用该 cookie 检查其他页面,如果可用,然后重定向到 JS 中的相同 URL? - How to store current URL in cookie, and use that cookie to check in other page, if available then redirect to the same URL in JS? 如何从用户那里获取输入(他们将输入链接)并将其存储在按钮内并重定向? - How can I get input(they will input a link) from user and store it inside of a button and have it redirect? 根据用户输入的URL自动重定向到自动完成字段 - Redirect to a url based on user input to an autocomplete field 从用户获取输入,存储并记录该输入 - Getting an input from user,store and log that input 检查 cookie 并使用 jQuery 重定向用户? - Check cookie and redirect user with jQuery? 将 URl 添加到 cookie 并在返回时重定向到 URL - Add URl to cookie and redirect to URL on return 如何根据他来自的 URL 将用户重定向到另一个 URL - How to Redirect User to Another URL Depending on the URL He Came From Vue.js和Vuex-将访问令牌存储在商店中,并将用户重定向到URL进行登录 - Vue.js & Vuex - Store access token in store and redirect user to an URL for login 从URL中获取用户名,然后使用带有该用户名的查询字符串进行重定向 - Take the user name from the URL and redirect with a query string with that user name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM