0

Please look at this code:

<script>
var mygrid;
function lock(){
    for (var i=1; i<15; i++)
    {
        var cur_row=i + "";
            mygrid.lockRow(cur_row,true);
            mygrid.setRowColor(i,"#E5E5E5");
    }
}
function doInitGrid(){
mygrid = new SomeClass;
}
</script>
</head>
<body onload="doInitGrid()" dir=rtl>
<div id="mygrid_container" style="width:905px;height:550px;"></div>
<script>lock();</script>
  <button onclick="addRow()">Add Row</button>  
  <button onclick="removeRow()">Remove Row</button>
  <button onclick="lock()">lock Row</button>
</body>

Why when I run lock function (Without the button), my var is undefined, and when I click on the button everything is ok?

0

6 Answers 6

2

This is a timing issue, not a scope issue.

You call doInitGrid() only onload so it won't assign a value to mygrid until after the document has finished loading.

When you call lock() inline, you do so as the document loads.

Presumably you have waited until the document has finished loading before clicking on the button.

Sign up to request clarification or add additional context in comments.

4 Comments

So how can I call lock at the correct timing? is there any solution?
Move to call to just after you make the assignment in the onload event handler.
But i have a problem that only after i called <div id="mygrid_container" style="width:905px;height:550px;"></div> I can do a lock, So I need the function to be called after the div is exist..
Which it will if you do what I said.
0

If by "my var is undefined" you mean that the variable mygrid is undefined, that's because you initialise it inside the function doInitGrid() which is not called until onload of the page. The onload event happens after the whole page has finished loading. Other inline script runs when the browser encounters it as it parses the document.

Comments

0

I am not sure when you have called the lock function without clicking button. But it appears that that the doInitGrid function was not executed when you have executed the lock function. The below code may work

function lock(){ 
    if(!mygrid){
      doInitGrid();
    }
    for (var i=1; i<15; i++) 
    { 
        var cur_row=i + ""; 
        mygrid.lockRow(cur_row,true); 
        mygrid.setRowColor(i,"#E5E5E5"); 
    } 
} 

Comments

0

You are calling load() before the document gets loaded completely. So your variable is not assigned as doInitgrid is not yet called.(it gets called only after document is fully loaded)

Comments

0

You are instantiating mygrid inside the function doInitGrid() which is called only on onload. But your script statement <script>lock();</script> executes before the page is loaded as it is part of the page HTML. When this script block executes, the value of mygrid is undefined which is the default value set by JS for all variables declared without an initial value.

If you want to call lock() when the page loads up, call it on onload after the call to doInitGrid() like this:

<body onload="doInitGrid(); lock();" dir=rtl>

2 Comments

But i have a problem that only after i called <div id="mygrid_container" style="width:905px;height:550px;"></div> I can do a lock, So I need the function to be called after the div is exist.
Onload will be triggered only after all the elements in your HTML are loaded. So it is safe to call lock() in your onload, since the div you are mentioning is guaranteed to be loaded by then.
0

Quentin has answered your question, But I'd like to add that it would be better to clean up your code a bit. Something like this.

<script>

function lock(mygrid){ //pass in the grid var
    for (var i=1; i<15; i++)
    {
        var cur_row=i + "";
            mygrid.lockRow(cur_row,true);
            mygrid.setRowColor(i,"#E5E5E5");
    }
}

function getInitGrid(){
   //some initialization code here maybe. Otherwise just take this out
   return new SomeClass;//return an instance
}

var mygrid = getInitGrid();

//and then pass in the mygrid variable wherever you call lock

lock(mygrid);
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.