2014-04-15 3 views
0
function form() 
{ 
var formVal1=document.forms ["form1"]["num1"].value; 
var formVal2=document.forms ["form1"]["num2"].value; 



if (formVal1<1 || formVal1>100) 
{ 
    alert("Please enter a value between 1-100"); 
    document.form1.num1.focus() ; 
    return false; 
} 
else if (formVal2<1 || formVal2>100) 
{ 
    alert("Please enter a value between 1-100"); 
    document.form1.num2.focus() ; 
    return false; 
} 


var sum= ((document.forms ["form1"]["num1"].value - 0) + (document.forms ["form1"]["num2"].value - 0)); 
     alert("Sum of two numbers:"  +sum); 
if(sum>0) 
{ 
var fromVal3=prompt("Please enter the third value:"); 
if(fromVal3<1 || fromVal3>5) 
{ 
alert("Please enter a value between 1-5"); 
document.form1.num3.focus() ; 
return false; 
} 

var Mul=fromVal3*sum; 
alert("Multiplied Value:" +Mul); 

} 

if(typeof(Storage)!=="undefined") 
    { 
    document.cookie=Mul; 
    alert(document.cookie); 
    var allcookies=document.cookie; 
    document.write(allcookies); 
    } 
    else 
    { 
    document.getElementById("result").innerHTML="Sorry, your browser does not support web storage..."; 
    } 


} 

두 번째 입력에 대해 두 개의 입력과 프롬프트를 취하는 첫 번째 두 숫자의 합계를 곱하여 그 결과를 쿠키로 설정하고 쿠키를 표시해야하는 javascipt 양식 hmtl 페이지입니다. 새 페이지에. 누구든지 쿠키를 설정하고 새 페이지에 표시하는 데 도움이 될 수 있습니까 ?? 지금까지와 같은쿠키를 설정하고 새 페이지에 표시

document.cookie='Mul='+Mul; 

:

+0

프롬프트 상자에서 처리 된 값에서 쿠키 값을 설정하고 새 페이지 – abhilash

답변

0

자바 스크립트 쿠키를 설정하려면 같은이

document.cookie="username=John"; 

또는

document.cookie="username=Joh"; expires=...; path=..."; 

따라서, 귀하의 예제에서, 그것은 것 같은 뭔가 뭔가가 필요 쿠키를 가져 오는 것은 모두 document.cookie이며, 이는와 같습니다.은 필요한 것을 얻기 위해 몇 번에 걸쳐 분할해야한다는 것을 의미합니다. 다음과 같은 내용 :

var c = document.cookie.split('; '); 
for (i=0;i<c.length;i++) { 
    var cookie = c[i].split('='); 
    if (cookie[0]=='Mul') { 
    var myCookie = cookie[1]; 
    break; 
    } 
} 

이 다음에 쿠키가 myCookie 변수에 저장됩니다.

+0

에 표시하면 정의되지 않은 쿠키가 표시됩니다. 실제로 3 번째 입력은 프롬프트 상자에서 가져옵니다. – abhilash

관련 문제