Do you test the code through file protocol like file:///C:/...
? If so, I can reproduce the situation you encountered.
Your code is right. But cookies are not set when browsing using the file:///
protocol, depending on the browser. You need to run your page on a http/https
server then the code can work on all browsers.
Result:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…