Jump to content

Why is variable not set here?


jekillen

Recommended Posts

I shouldn't have to do this here but

 

I have written this function as a function for ajax query to run on completion of query.

and there is a with(document.forms.formName) block in which a variable should have

been set by preceding code and has not. it is marked with comment: 'BLOCK IN QUESTION'

in this block target has lost its value. I have done the same thing elsewhere, frequently in the

same project with success. I don't see why target is loosing its value inside the with block;

 

Target should be set in the else if block just prior to the with block.

 

This is part of a cms system file editor that would be processing the creation of a save as

file from ajax query. This function should execute submit to open the new saved as file.

But the hidden field used to find the file on the server needs to be valid.

 

(forum software is putting icons in place of instances of ' B)')

 

var mkSaveAsFile = function(c, d) // c: return from ajax query, d: display element id { if(c && openSaveAsOnReturn == 'true' && c.indexOf('error:') < 0) {

var showRes = function(a, B) { var tar = document.getElementById( B) if(!tar) { alert('Display element '+b+' not found'); return; } if(a.indexOf('error') >= 0) { tar.style = "color:#cc3333"; } tar.childNodes[0].data = a } var target = '';

// *** SOLUTION: was to open the with block here and include the two conditional blocks //var tst = (saveAsFileName != saveAsOriginalName); //alert('New '+saveAsFileName+' old:'+saveAsOriginalName+' tst: '+tst) // checks out good if(!saveAsFileName && saveAsOriginalName.indexOf(browserVals['Save As'].replace(base, '')) < 0) { var getOldName = new _getPathInfo(saveAsOriginalName, true); var getNewName = new _getPathInfo(saveAsFileName, true); if(getOldName.basename == getNewName.basename && getOldName.dirname != getNewName.dirname) { target = saveAsOriginalName; } } else if(saveAsFileName && (saveAsFileName != saveAsOriginalName)) { target = saveAsFileName; // <<<< target set here alert('1:target: '+target) // checks out } // BLOCK IN QUESTION with(document.forms.script) { act.value = 'getFile'; alert('2:target '+target) // target == '' if(browserVals['Save As'] != '') { doFile.value = 'UED-'+browserVals['Save As'].replace(base, '')+'/'+target; } else { doFile.value = 'UED-/'+target; } if(confirm('doFile: '+doFile.value)) { //alert('should submit now: doFile: '+doFile.value) submit() //showRes(c, d) } else { c += ': submission aborted' showRes(c, d) } } } else { showRes(c, d) } }

Edited by jekillen
Link to comment
Share on other sites

2 things. One is that there is a case when target will still be empty. If that's expected, then it's not a problem. The other issue is that forms have a target property. Since you are using that with statement, then inside that block target is pointing to the form's property instead of the local variable. That's one reason why with statements aren't used very often. You can save the form in another variable and use that instead of with, or you can use the long version to refer to the form, or you can rename the variable to use something that isn't a property of the form.

(forum software is putting icons in place of instances of ' B)')

If you used code tags it wouldn't do that.
var showRes = function(a, B)
Link to comment
Share on other sites

Thank you for info about target property in form object.

I was thinking I may have also created another form element with name 'target'

and that was being used due to conflict.

 

As it turns out, for some reason the with block opened in that location was masking out the target variable.

I moved the opening of the with block to include the two conditional statements within it. Now it works

 

As a commentary about using with(): I have been writing a single eventHandler function that uses e.target.id

to figure out what to do, in a switch block. The switch block often contains code to set and revise hidden fields

and do other form related processing, so using with() allows me to not have to use literal document.form reference

every time a form element has to be accessed. These references may be in the dozens, spread accross involved

case labels. True, it makes it abit harder to debug, having to remember that a particular reference is to a form element.

 

Thank you for time and patience. Sometimes I post due to frustration when I should give it a bit more thought

Link to comment
Share on other sites

I was thinking I may have also created another form element with name 'target'and that was being used due to conflict.

That's another issue with using the with statement, it's why you don't see them much.

I moved the opening of the with block to include the two conditional statements within it. Now it works

Unless you also moved the line where you define the variable target, you may have only just switched to changing the form's target instead of using the local variable.

using with() allows me to not have to use literal document.form referenceevery time a form element has to be accessed

You don't have to do that anyway.
var f = document.forms.script;
Now use f anywhere you need to refer to the form or one of its properties or methods, and you have no problems with conflicting variables.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...