Javascript setting variables and object and proper syntax (debug console)

So I am trying to invoke methods on a page and I know the values that I want to put inside the methods but I can't seem to get the syntax to work. I feel so, well.. lamerized.

Ok so here is the javascript method on the page

function ReturnValue (sValue, sText) 
{
     window.focus();
     var oForm = document.EditForm;

     switch (szType)        // Form element name
     {
     case 'agt':
        oForm.agt.value = sText;
        oForm.agentman.value = sValue;
        oForm.agtid.value = sValue;                              
        oForm.getagt.focus();
        break;

     case 'county':     
        oForm.County.value = sValue;
        sCurrentCounty = new String(sValue);
        document.all("CountyDisp").innerHTML = sText;
        document.all("City").value = "";
        document.all("CityDisp").innerHTML = "";
        document.all("Area").value = "";
        document.all("AreaDisp").innerHTML = "";
        break;
   default:
        break;      
     }  // End switch
 return;
}

Very straight forward function and you would assume that the parameters were strings, right? So in the IE8 Script Debugger Console I tried this:

ReturnValue("adf","asdf"); //FAIL "Object expected"

Object expected huh, well maybe I need single quotes for the strings I assumed next (just in case).

ReturnValue('adf','asdf'); //FAIL "Object expected"

Okay, just making sure.. So I need an object that stores a string. How about using a var I thought..

var o = "adf"; var p = "dfsa"; ReturnValue(o,p); //FAIL "Object expected"

I tried with single quotes just to be sure. So after all that I am sure an object is needed. So I tried to create an Object.

o = new Object(); k = new Object(); //{...}

Now I from here I didn't know how to add a string to an object so I just did this.

o.value = "text"; k.value = "field"; // "text" ... "field"

Okay so now I am feeling excited I have an object with some string in there so now I try to put it all together again.

ReturnValue(o,z) // EPIC FAIL "Object expected"

I am putting Objects in there! Now I'm back to square one, can someone help?

Okay problem still not solved.

Upon further investigation I found that the script does infact run once at the very beginning of the page load. I can debug and break and while its paused through the code I can run the methods. But after I release and it finishes declaring all the variables I can't run any methods. But, for some reason the same method that I am trying to run is able to run from a Popup using the Window.Opener.ReturnValue(string,string);

I dont get it!

Javascript guru's where are you when I need you!

Answers:

Answer

Assuming you've defined szType somewhere else, I pasted this function into a scratch page and played around with it and I can't reproduce this issue in IE8 (or FireFox 3).

Googling around, the only thing I found was a reference saying that if you set the script type incorrectly you might get this as IE won't parse the script block at all. Is your script block set as "text/javascript"?

If that doesn't work, can you provide more of the code involved (perhaps the markup too)?

On another note, you might want to look at using something like jQuery or one of the other JS libraries. Browser-specific JS like this is evil (death to document.all).

Answer

Perhaps szType, one of oForm, oForm.agt etc is null?

Answer

What's the value of szType? I can't see it being set anywhere, maybe that's what it is bombing on? Also, those document.all and form. probably won't work in anything other than IE. document.getElementById('theid') is much better for retrieving dom elements.

Also, can you try setting a breakpoint on the ReturnValue function and step through it? I haven't used the IE8 console this way, but I know you can debug that way.

edit Is it able to execute functions that way? Maybe comment out everything in ReturnValue and make sure it can actually execute the function itself. You might have to be in a debug session, or invoke the function via window.ReturnValue

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.