Solved

Setting global values in Javascript

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

19 Experts available now in Live!

Get 1:1 Help Now