Solved

Setting global values in Javascript

Posted on 2011-03-10
3
605 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now