Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setting global values in Javascript

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

824 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