2016-03-31 6 views
2

Ich versuche, ein Cookie in einem Eingabefeld zu speichern, das funktioniert gut in Chrome, aber es funktioniert nicht auf IE-11. Kann mir jemand sagen, was ich vermisse, damit dieser Cookie auch auf IE funktioniert? Hier ist mein Code. Danke im Voraus.Speichern eines Cookies auf IE funktioniert nicht

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

Ihre Form genannt wird 'myform' aber Ihr Code bezieht sich auf' document.homeForm ... ' - Ist das ein Typ in deinem Code oder nur dieser Post? – Emissary

+0

@Emissary nur diesen Beitrag – progx

+0

Dieser Code funktionierte gut auf IE 11 für mich. Haben Sie Cookies aktiviert? – gmfm

Antwort

2

@progx sind hier einige Bilder von dieser für mich arbeiten auf IE 11. Aber nur Cross-Browser-Kompatibilität versichern Sie Ihre Attributwerte einschließen sollten in Zitate.

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

vs

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

Unten ist der Code wie verwendet und Bilder des Arbeitscodes. -Code verwendet:

<!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

Vielen Dank für Ihre Hilfe und Zeit !! – progx

+0

kein Problem, ich hoffe es hat geholfen. – gmfm

+0

ja hat es getan. Ich gebe dir diese 50 Punkte :) – progx

Verwandte Themen