Solved

Setting global values in Javascript

Posted on 2011-03-10
3
625 Views
Last Modified: 2012-05-11
Hello Experts,

What I want to do:
Have the user select a link to call a javascript to perform some calculation and assign some style to a label then display that value in a label.

What I have:
1.      I have a master page with a couple of anchors each having  an onclick that passes a value to a Javascript function called ‘settab’ then redirects the page.
2.      This function changes the contents of the global value.
3.      On the body tag I call an onload that calls a Javascript function which reads the global variable and displays the value to a label called ‘setlabel’.  

The problem:
I started out using hidden inputs, however  the content I load to the hidden value in settab is not yet written to the browser at the time the onload function setLabel is executed , so I end up reading the original initialized content of the global variable when doing a document.getElementById("hidTab").value.
So I went to global values rather than hidden inputs.  ‘settab’ loads the global value which is good.  But when the onload function is called the global value is initialized and the New value is lost.

Clearly I have a timing issue because I am setting values at the time the anchor is selected, but those values are not available until the page is actually written for  a hidden value or the global are initialized when before the page is written.

My Question:
So Now I am looking at code behind.
1.        If I set the Global variable at the time an anchor is selected will I be able to access that in code behind?  
2.      If so, would I be able to set the value and read it in the Onload javascript function so I can set my variable?
3.      If so, do you have any snippets of the code behind?
4.      If Not so, Any ideas?

Thanks in advance.

I have included my really scaled down java script.

<body onload="findTab()">
    <form id="form1" runat="server" >
    <div>
        <input id="hidTab" type="hidden" value="1" />
              <div>
               <ul class="theMenu">                                
   <li><a runat="server" onclick="setTab('0')" id="A0"  
    href="../maintenance/EmployeeAdd.aspx"> add      
     new</a> </li>
                     <li><a runat="server" onclick="setTab('1')" id="A1"
                            href="../maintenance/EmployeeMaintenance.aspx">
                            maintain</a> </li>
                     <li><a runat="server" onclick="setTab('2')" id="A2"
                      href="#">timesheets </a></li>
                     <li><a runat="server" onclick="setTab('3')" id="A3"  
                            href="#">expenses </a></li>
                     </ul>
               </div>
                       
               <asp:ContentPlaceHolder ID="employees" runat="server">
                   </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>


var newtab = "";

function setTab(elem) {

 
    var g = document.getElementById("hidTab").value;
    if (g == null)
    { return };
    if (elem == null)
    { return };
    document.getElementById("hidTab").value = elem;
    newtab = elem;


}


function setLabel() {

   
    var elem = newtab;
    var a = "ctl00_MainTab_TabPanel1_A" + elem;

    varhd = document.getElementById("hidTab").value;
 
    for (var k = 0; k < 10; k++) {

        var b = "ctl00_MainTab_TabPanel1_A" + k;
        if (a == b) {
            document.getElementById(b).style.color = '#FFFFFF';
        }
        if (elem != k) {
            document.getElementById(b).style.color = '#000000';
        }

    }
}
0
Comment
Question by:nowmbie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35095987
A cookie should be able to resolve this issue.  Cookies are available to the JavaScript as well as the code behind.
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35096341
Hi,

You have to declare variable with var keyword at the time of starting the <script> tag. Then you have to use this variable into your whole javascript code.
0
 

Author Closing Comment

by:nowmbie
ID: 35097038
Thanks Much!  Cookies Cookies Cookies! I will never forget!
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question