簡體   English   中英

設置cookie並顯示在新頁面中

[英]Set cookie and display in new page

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頁面,需要兩個輸入並提示輸入第三個,它將與前兩個數字的和相乘並將結果設置為cookie,並且必須在新頁面上顯示cookie。 誰能幫助我設置Cookie並將其顯示在新頁面上?

要設置JavaScript cookie,您需要這樣的內容

document.cookie="username=John";

要么

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

因此,在您的示例中,它將類似於:

document.cookie='Mul='+Mul;

就獲得cookie而言,您所擁有的只是document.cookie ,它看起來像a=b; c=d; e=f a=b; c=d; e=f a=b; c=d; e=f意味着您需要分裂幾次才能獲得所需的東西。 像這樣:

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;
  }
}

之后,您將把cookie存儲在myCookie變量中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM