Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting global values in Javascript

Posted on 2011-03-10
3
Medium Priority
?
635 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

670 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