在Laravel页面刷新中删除jQuery中的cookie

时间:2015-12-07 23:11:31

标签: javascript jquery cookies laravel-5.1

我试图在页面刷新时删除cookie但我无法实现它。到目前为止我已经这样做了

//delete the cookie on page refresh
window.onunload = unloadPage;
function unloadPage()
{
    $.removeCookie('count', { path: '/' });
}

此代码位于我的js文件的顶部。 我也在stackoverflow

尝试了答案

更新 我正在使用laravel,我正在使用此代码来设置cookie

$response->withCookie(cookie()->forever('count', $count));

3 个答案:

答案 0 :(得分:1)

document.cookie = 'count=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

答案 1 :(得分:1)

所以,我必须通过这种方式删除cookie

/**
     * delete the count cookie
     */
    public function destroyCookie(){
        $cookie = Cookie::forget('count');
        $response = new Response($cookie);
        $response->withCookie($cookie);

        return $response;
    }

并在我的控制器方法中

Route::post('remove-cookie', 'DonnerController@destroyCookie');

路线

public class FileReaderProgram
 {

  public static void main(String[] args) throws IOException
 {
ArrayList<String> words = new ArrayList<String>();
ArrayList<Integer> numbers = new ArrayList<Integer>();
String stringComponent = " ";
int integerComponent = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter the absolute path of the file");
String fileName = in.next();             //Gets the file from the user
File inFile = new File(fileName);
**Map<String,Integer> lineCombo = new TreeMap<String,Integer>();**

try
{
   Scanner fileReader = new Scanner(inFile); //Constructs Scanner for reading the file
   fileReader.useDelimiter("\\n");
   while (fileReader.hasNextLine())
   {
     String line = fileReader.nextLine();                  //Gets line from the file
     Scanner lineScanner = new Scanner(line);    //Constructs new scanner to analyize the line

     lineScanner.useDelimiter(",");     

     stringComponent = lineScanner.next();     //Read first word
     while (!lineScanner.hasNextInt())
     {
       stringComponent = stringComponent + " " + lineScanner.next();  //Checks if more than one word part of string
     }
     integerComponent = lineScanner.nextInt(); //Read in integer


     words.add(stringComponent);    //Array of Strings, element number corresponding to the line it came from
     numbers.add(integerComponent);  //Array of Ints, same thing as above

     **lineCombo.put(stringComponent, integerComponent);**


   }
}

我希望它可以帮助一些使用laravel的人。

答案 2 :(得分:0)

您不需要使用unload事件,因为每次刷新时,标题中包含的每个脚本都将从上到下执行。

正因为如此,您只需要在脚本顶部进行简单检查,以验证当前URL是否与以前相同,如果您想确保页面确实已刷新。< / p>

这是一个例子(未经测试):

if (document.referrer == window.location.href) {
    // The page was refreshed.
}

希望这有帮助!