2016-03-31 51 views
2

我想在一個輸入欄中存儲一個cookie,在Chrome上工作得很好,但它不能在IE-11上工作。任何人都可以告訴我我錯過了什麼,所以這個cookie也可以在IE上運行?這是我的代碼。先謝謝了。存儲在IE不工作的cookie

的Javascript:

function setCookie(key, value) { 
var expires = new Date(); 
expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000)); 
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString(); 
} 

function getCookie(key) { 
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)'); 
return keyValue ? keyValue[2] : null; 
} 

function myfunction() { 
setCookie("input1", '1'); 
alert(getCookie("input1")); 
document.homeForm.input1.value = getCookie("input1"); 
} 

HTML

<form name="myform"> 
<input type=text name=input1 value=""/> 
</form> 
+0

您的形式被稱爲'myform'但你的代碼是指'document.homeForm ...' - 是你的代碼或只是這個職位的類型? – Emissary

+0

@Emissary只是這個職位 – progx

+0

這段代碼對我來說工作得很好。你有cookies嗎? – gmfm

回答

2

@progx這裏是爲我工作的IE瀏覽器11.但爲了確保跨瀏覽器兼容,你應該附上你的屬性值一些圖片引號。

<input type="text" name="input1" value=""/> 

VS

<input type=text name=input1 value=""/> 

下面是代碼所使用和工作碼的圖像。使用 代碼:

<!DOCTYPE html> 
<html> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
<script> 
$(document).ready(function() { 
    myfunction(); 
}); 

function setCookie(key, value) { 
var expires = new Date(); 
expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000)); 
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString(); 
} 

function getCookie(key) { 
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)'); 
return keyValue ? keyValue[2] : null; 
} 

function myfunction() { 
setCookie("input1", '1'); 
alert(getCookie("input1")); 
document.myform.input1.value = getCookie("input1"); 
} 
</script> 
<style> 

</style> 
<body> 
<form name="myform"> 
<input type=text name=input1 value=""/> 
</form> 
</body> 
</html> 

alert image

input image

editor image

console image

+0

非常感謝您的幫助和時間! – progx

+0

沒問題,我希望它有幫助。 – gmfm

+0

是的,它做到了。我給你50分:) – progx