Problem with JavaScript closure.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Problem with JavaScript closure.

    Code:
    var previous_obj = null;
    var previous_url = null;
    function loadPage(cur_obj,url){
    previous_obj.onclick = function(){loadPage(this,previous_url);}
    previous_obj.className = "header_inactivated";
    cur_obj.className = "header_activated";
    cur_obj.onclick = function(){};
    previous_obj = cur_obj;
    previous_url = url;
    window.frames['main_frame'].location.href = url;
    }
    function set(){
    previous_obj = document.getElementById("own_alerts");
    previous_url = 'own_alerts.html';
    previous_obj.onclick = function(){}
    document.getElementById('main_frame').style.width = (screen.width-30)+"px";
    window.frames['main_frame'].location.href = 'own_alerts.html';
    }
    Code:
    <body onload="set()">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td class="header_activated" onclick="loadPage(this,'own_alerts.html')" id="own_alerts" 
    	title="List of alerts sent by you">Alerts sent by you</td>
        <td class="header_inactivated" onclick="loadPage(this,'others_alerts.html')" id="others_alerts"
    	title="List of alerts recieved by you">Alerts recieved by you</td>
        <td style="border-bottom-style:solid;border-bottom-color:#000000;border-bottom-width:2px">
    	&nbsp;
    	</td>
      </tr>
      <tr>
        <td colspan="3" class="full" style="height:7px;background-color:#BBBBBB"></td>
      </tr>
      <tr>
        <td colspan="3" class="full" style="height:700px">
    	<iframe src="" frameborder="0" height="690px" 
    	name="main_frame" scrolling="auto" id="main_frame"></iframe>
    	</td>
      </tr>
    </table>
    
    </body>
    </html>
    Here previous_url is not getting cloned ... How can i save the previous URL and pass it to the onclick handler of TD?
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Now it has been solved ;)
    Actually it was happening due to global reference ...
    I changed the code like this ...
    Code:
    function loadPage(cur_obj,url){
    var temp_url = previous_url;
    previous_obj.onclick = function(){loadPage(this,temp_url);}
    previous_obj.className = "header_inactivated";
    ......
    ......
    ......

    Comment

    Working...