他の人が言うように、Cookie は現在のページ要求に影響を与えないため、それを行う唯一の方法は JS または jQueryです。
jQuery ソリューションにはjquery cookie プラグインが必要です。一部のサーバーでは jquery.cookie.js に問題があります(解決策は、ファイルの名前を変更することです。例: jquery.cook.js)
jquery cookie プラグインの使用
セッション Cookie を作成します。
$.cookie('the_cookie', 'the_value');
有効期限が切れる Cookie を作成し、それから 7 日後:
$.cookie('the_cookie', 'the_value', { expires: 7 });
サイト全体で有効な期限切れの Cookie を作成します。
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
クッキーを読む:
$.cookie('the_cookie'); // => "the_value"
$.cookie('not_existing'); // => undefined
利用可能なすべての Cookie を読み取ります。
$.cookie(); // => { "the_cookie": "the_value", "...remaining": "cookies" }
クッキーを削除:
// Returns true when cookie was found, false when no cookie was found...
$.removeCookie('the_cookie');
// Cookie が書き込まれたときと同じパス...
$.removeCookie('the_cookie', { path: '/' });
localStorage を試すことができます。Chrome、FF、IE9 以降で動作します。IE7-10 はサポートしていません。万歳!
IE8 には localStorage に関するいくつかの問題があります。
スクリプトは $(document).ready(function() {}); 内にある必要があります。
$(document).ready(function() {
$("#btnClick").click(function(e) {
e.preventDefault();
localStorage.setItem('cookieName', 'cookie_value');
window.href.location = "your_new_page.php";
});
//On the same page or other page
if (localStorage.getItem('cookieName')){
//do here what you want
}else{
//do something else
}
});