简体   繁体   English

Javascript SyntaxError:意外的标记'}'

[英]Javascript SyntaxError: Unexpected token '}'

I have the following simple code, however for some reason it trows a Unexpected token "}" error, and I'm not entirely sure why. 我有以下简单的代码,但由于某种原因它会出现意外的令牌“}”错误,我不完全确定原因。 I have the following code to generate the buttons 我有以下代码来生成按钮

var OverlayMessage = ("Do you want to test these settings? <br> <h6>" + protocol + ":" + systemcodeSend + "-" + unitcode + " sending " + state + '</h6> <br> <a id="btnGreen" onclick="checkIfSettingsWork("' + protocol +'", "'+ systemcodeSend +'", "'+ unitcode +'", "'+ state +'");">Yes</a> <a id="btnRed" onclick="toggleVisibility(\'overlayAddDevice\')">No</a>');
        console.log(OverlayMessage);
        overlayMessage(OverlayMessage);

The output of console.log is as followed console.log的输出如下

<a id="btnGreen" onclick="checkIfSettingsWork("pollin", "31", "14", "on");">Yes</a>

The strange thing is, if I run from the debugger, it works fine without errors. 奇怪的是,如果我从调试器运行,它可以正常工作而不会出错。 However when being ran trough the onclick event it trows the Unexpected token error. 但是,当通过onclick事件运行时,它会拖出意外的令牌错误。

The function checkIfSettingsWork is not even being ran, I checked this by letting it log to console, and that is returning nothing when being ran by the onclick event however it works when I run the following javascript trough the debugger. 函数checkIfSettingsWork甚至没有运行,我通过让它登录到控制台检查了这一点,并且当被onclick事件运行时没有返回任何内容,但是当我通过调试器运行以下javascript时它可以工作。

checkIfSettingsWork("pollin", "31", "14", "on");

This is the checkIfSettingsWork function. 这是checkIfSettingsWork函数。

function checkIfSettingsWork(protocol, systemcodeSend, unitcode, state) {
    console.log(protocol + systemcodeSend + unitcode + state)

}

This is invalid html: 这是无效的html:

 <a id="btnGreen" onclick="checkIfSettingsWork("pollin", "31", "14", "on");">Yes</a>

The " characters are ending your onclick attribute early, so the browser ends up interpreting it as just onclick="checkIfSettingsWork(" . "字符提前结束你的onclick属性,所以浏览器最终将其解释为onclick="checkIfSettingsWork("

Change it to this for a workaround: 将其更改为此解决方法:

<a id="btnGreen" onclick="checkIfSettingsWork('pollin', '31', '14', 'on')">Yes</a>

有效的synatax将是

<a id="btnGreen" onclick="checkIfSettingsWork('pollin', '31', '14', 'on')">Yes</a>

You made a low-level mistake. 你犯了一个低级别的错误。

This is invalid like onclick="checkIfSettingsWork("pollin", "31", "14", "on");" 这是无效的,如onclick="checkIfSettingsWork("pollin", "31", "14", "on");"

You need fix " to ' . 你需要修复“到”。

Another thing is i think when you declaring OverlayMessage , just my suggestion, maybe you need use " and ' characters by uniform way. 另一件事是我认为当你宣布OverlayMessage ,只是我的建议,也许你需要通过统一的方式使用"'字符。

You have many contexts going on here and I think they need to be separated. 你有很多背景,我认为他们需要分开。 You are trying to create text, HTML, and JavaScript in this one declaration. 您正在尝试在此声明中创建文本,HTML和JavaScript。 Also in you mixed single and double quotes. 还有你混合的单引号和双引号。 In your concatenation. 在你的连接中。 You could use both but they have to match. 您可以使用两者,但必须匹配。

It's not safe to do all these in one line. 在一行中完成所有这些并不安全。 Especially the onClick function. 特别是onClick功能。 I don't want to create a template for the HTML, right out for a JavaScript function, then feed the variables into the function. 我不想为HTML创建模板,直接用于JavaScript函数,然后将变量提供给函数。

You also don't need any quotes for your parameters in your function you could just feed in the variables. 您也可以在函数中为参数添加任何引号,只需输入变量即可。

CheckIfSettingsWork( protocol, systemcodeSend, unitcode, state) CheckIfSettingsWork(protocol,systemcodeSend,unitcode,state)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM