Javascript 设置变量和对象以及正确的语法(调试控制台)

发布于 2024-07-26 09:21:09 字数 2010 浏览 3 评论 0原文

因此,我尝试在页面上调用方法,并且我知道要放入方法中的值,但我似乎无法使语法正常工作。 我感觉是这样,好吧..层层剥落。

好的,这是页面上的 javascript 方法

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;
}

非常简单的函数,您会假设参数是字符串,对吧? 所以在 IE8 脚本调试器控制台中我尝试了以下操作:

ReturnValue("adf","asdf"); //失败 “预期目标”

预期对象嗯,也许我接下来假设的字符串需要单引号(以防万一)。

ReturnValue('adf','asdf'); //失败 “预期目标”

好的,只是确保..所以我需要一个存储字符串的对象。 使用我想的 var 怎么样?

var o = "adf"; var p = "dfsa"; 返回值(o,p); //失败“预期对象”

我尝试使用单引号只是为了确定。 所以毕竟我确信需要一个对象。 所以我尝试创建一个对象。

o = 新对象(); k = 新对象(); //{...}

现在我不知道如何向对象添加字符串,所以我就这么做了。

o.value = "文本"; k.value = "字段"; //“文本”...“字段”

好吧,现在我感到很兴奋,我有一个带有一些字符串的对象,所以现在我尝试再次将它们组合在一起。

返回值(o,z) // 史诗失败“预期对象”

我正在将对象放在那里! 现在我又回到了第一点,有人可以帮忙吗?

好吧问题还是没有解决。

经过进一步调查,我发现该脚本实际上在页面加载一开始就运行一次。 我可以调试和中断,当代码暂停时我可以运行这些方法。 但是在我发布并完成声明所有变量之后,我无法运行任何方法。 但是,由于某种原因,我尝试运行的相同方法可以使用 Window.Opener.ReturnValue(string,string); 从弹出窗口运行。

我不明白!

当我需要你的时候,Javascript 大师你在哪里!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心舞飞扬 2024-08-02 09:21:09

假设您已在其他地方定义了 szType,我将此函数粘贴到草稿页中并进行了测试,但无法在 IE8(或 FireFox 3)中重现此问题。

谷歌搜索,我发现的唯一一件事是一个参考说,如果你错误地设置了脚本类型,你可能会得到这个,因为 IE 根本不会解析脚本块。 您的脚本块是否设置为“text/javascript”?

如果这不起作用,您能否提供更多涉及的代码(也许还有标记)?

另一方面,您可能想考虑使用 jQuery 或其他 JS 库之一。 像这样的特定于浏览器的 JS 是邪恶的(document.all 的死亡)。

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).

尘曦 2024-08-02 09:21:09

也许 szType、oForm、oForm.agt 等之一为 null?

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

深海夜未眠 2024-08-02 09:21:09

szType 的值是多少? 我看不到它被设置在任何地方,也许这就是它正在轰炸的地方? 另外,那些 document.all 和 form. 可能无法在 IE 以外的任何环境中运行。 document.getElementById('theid') 更适合检索 dom 元素。

另外,您可以尝试在 ReturnValue 函数上设置断点并单步执行吗? 我没有这样使用过 IE8 控制台,但我知道你可以这样调试。

编辑 是否能够以这种方式执行功能? 也许注释掉 ReturnValue 中的所有内容,并确保它实际上可以执行函数本身。 您可能必须处于调试会话中,或者通过 window.ReturnValue 调用该函数

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文