2017-08-15 138 views
0

我需要幫助在兩個aspx頁面之間傳遞數據(這些aspx頁面存儲在SharePoint Designer中)。我創建了一些JavaScript,可以對我們網站上的指標進行一些簡單的計算。使用javascript在兩個aspx頁面之間傳遞數據

https://tandemhospitalpartners.sharepoint.com/SitePages/Home.aspx

下面的JavaScript是在上面列出的ASPX鏈接。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
 
        "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
 
<head> 
 
    <script type="text/javascript"> 
 
     window.onload = function() 
 
     { 
 

 
     } 
 
     
 
     function p1() 
 
     { 
 

 
      totalPts = 137; 
 
      setTotalPts(totalPts); 
 
      
 
      
 
      divId = document.getElementById('avg'); // element represents the ID 'countdown' for the div 
 

 
      today = new Date(); //this represents todays date in the following format 
 
      //Weekday ex. "Tue" 
 
      //Month ex. "Aug" 
 
      //Todays Date ex. "08" 
 
      //Year ex. "2017" 
 
      //Time ex. "13:44:54 GMT - 0500 (Central Daylight Time" 
 

 

 
      svOpen = new Date(2017, 06, 17); // this represents the first day we accepted patients 
 
      
 

 
      one_day = 1000 * 60 * 60 * 24; 
 
      //milliseconds 
 
      //minutes 
 
      //seconds 
 
      //hours 
 

 

 
      //We have been open for 23 days 
 

 

 
      daysOpen = Math.ceil((today.getTime() - svOpen.getTime())/(one_day)); 
 
      //subtracting SVNH open date from current date 
 

 

 

 

 

 

 
      
 

 

 
      
 

 
      avgPts = parseFloat(totalPts)/parseFloat(daysOpen); 
 

 
      
 
      divId.innerHTML ="Average Patients Per Day: "+ avgPts.toFixed(1); 
 
     } 
 
     function GetDays(element) 
 
     { 
 
      
 
      var el = document.getElementById(element); 
 
      today = new Date(); 
 
      var cmas = new Date(today.getFullYear(), 6, 12); 
 
      if (today.getMonth() == 6 && today.getDate() > 13) { 
 
       cmas.setFullYear(cmas.getFullYear() + 1); 
 
      } 
 
      var one_day = 1000 * 60 * 60 * 24; 
 
      el.innerHTML ="&nbsp;&nbsp;Days to open the second Hospital: &nbsp;&nbsp;&nbsp;"+Math.ceil((cmas.getTime() - today.getTime())/(one_day)); 
 
     } 
 
     function setTotalPts(totalPts) 
 
     { 
 
      totId = document.getElementById("leOne"); 
 

 
      totId.innerHTML = "This is my total patients: " + totalPts.toString(); 
 

 

 
     } 
 

 
    </script> 
 
</head> 
 
<body onload="p1(); GetDays('count'); setTotalPts();"+"text"> <!-- this onload is passing the div ID to the function p1(element) 
 
             element represents the ID --> 
 

 
<div id='count' style="color: #e51400 ; 
 
font-weight : 500;"></div> 
 
</body> 
 

 

 
<body> 
 

 
<div id='avg' style="color: #e51400 ; 
 
font-weight : 500;"></div> 
 

 

 

 
</body> 
 

 
<body> 
 

 
<div id='leOne' style="color: #e51400 ; 
 
font-weight : 500;"></div> 
 

 

 

 

 
</body> 
 
</html> 
 

 

 
<!-- to keep a running count of the days i will have to subtract svOpen from todaysDate

我的目標是totalPts傳遞到HTML的ID是在不同的aspx文件。

這是包含我想要發送它的HTML ID的aspx文件的鏈接。

https://tandemhospitalpartners.sharepoint.com/ININD/SitePages/Home.aspx

這是我真的很困惑。我不知道如何實現這個結果。如果任何人有關於如何使用JavaScript可以完成的想法,我將不勝感激!

回答

1

由於web是無狀態環境,因此無法從另一個頁面的客戶端訪問另一個頁面的html。但是從第一頁開始,您可以通過查詢字符串將數據傳遞到下一頁,也可以將數據保存到localStorage,然後導航到下一頁並從localStorage檢索數據。我更喜歡Querystring,因爲它看起來就像是你想傳遞的數字。

假設你有從查詢字符串在目標頁打開了第二頁

<asp:Button runat="server" ID="btnNext" OnClientClick="location.href='https://tandemhospitalpartners.sharepoint.com/ININD/SitePages/Home.aspx?totalPointes=' + getTotalPoints()" Text="Inind Home"></Button> 

然後totalPoints並將其保存到該元素的按鈕。例如

$("#SomeID").val(getvaluefromQuerystring()); 
+0

非常感謝你對你的迴應。如果你不介意,我仍然有幾個問題。我需要在第二頁上創建一個JavaScript函數嗎?我必須通過按鈕來做到這一點嗎?我必須在第一頁的JavaScript中創建一個getTotalPoints()方法嗎?從我的理解,它似乎像getTotalPoints()將是我的JS在第一頁上的方法,將有一個返回線,它返回我想要使用的變量? – hcienfue

0
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script 
    src="https://code.jquery.com/jquery-3.2.1.js" 
    integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" 
    crossorigin="anonymous"></script> 

    <script> 
     $(document).ready(function() { 
      $("#dvValue").html(getUrlParameter("totalPointes")); 
     }); 

     //This function gets the querystring value from the querystring 
     function getUrlParameter(name) { 
      name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); 
      var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); 
      var results = regex.exec(location.search); 
      return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); 
     }; 
    </script> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <div id="dvValue"></div> 
    </div> 
    </form> 
</body> 
</html> 
+0

我正在從querystring參數「totalPointes」中檢索值並將其值保存爲ID爲「dvValue」的div –