-3

Recently I found some interesting codes on a website.

 <div class="inputArea">
    <textarea type="text" id="textInput" class="chatInput lightBorder"></textarea>
</div>

<a href="javascript:;" class="chatSend" click="sendMsg@.inputArea"><b>Send</b></a>

When I click the "Send" button (it's a hyperlink, but it looks like a button on the page") and it will fire the js code "sendMsg@.inputArea". What it does is to send a message in the textarea to the server. It acts like the sendMsg is a function and .inputArea is a parameter passed to that function. But it does not seem to follow the EMAC standard. However, it works. How is it possible? It now looks like black magic to me. Can someone explain how the @ character works in the code?

Kehan Wang
  • 173
  • 1
  • 8

2 Answers2

3

Updated Answer

...now that you've shown what you think is "JavaScript code."

This isn't JavaScript code:

<a href="javascript:;" class="chatSend" click="sendMsg@.inputArea"><b>Send</b></a>
<!-- Not JavaScript Code ----------------------^^^^^^^^^^^^^^^^^^          -->

That's just an attribute value on an element. (And an invalid one, a doesn't have a click attribute.) Presumably code in their JavaScript understands what to do with it. You're confusing that with an onclick attribute, which would (normally) contain JavaScript code.

Original Answer

You can't use @ in a property name literal, variable name, or function name (collectively, an IdentifierName) in JavaScript.*

how to declare a special character in js

You can't. The characters allowed in IdentifierName are defined by the specification and are not extensible.

You can use any character you like as a property name (but not variable or function name), but not as a literal, only if you use brackets notation and a string, e.g.:

var obj = {"SendMsg@": "foo"};
console.log(obj["SendMsg@"]); // "foo"

* That said, JavaScript is very liberal about the characters you can use in IdentiferName, so while @ isn't allowed, I couldn't absolutely guarantee there isn't some other Unicode character that looks a bit like it that's allowed. But I suspect not, as the Unicode page for @ doesn't list any characters likely to be confused with @.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Sorry that I wasn't clear about the question, now I edited it. The code "sendMsg@.inputArea" seems to fire a function. In that function an AJAX post is send to the sever. But how can it possibly work because the @ is not allowed in a function name? – Kehan Wang Mar 11 '15 at 15:51
  • @KehanWang: What you're referring to isn't JavaScript code. I've updated the answer accordingly. – T.J. Crowder Mar 11 '15 at 15:54
  • Thank you. Can you be more specific? How the browser understands what to do when I click the button? – Kehan Wang Mar 11 '15 at 15:57
  • 1
    @KehanWang: It reads the attribute and then does something with it. That's all there is to it. I don't think it's worth my (or your) diving into their code to see *what* they do with it; fundamentally, it's just an identifier in their custom system, not JavaScript code. – T.J. Crowder Mar 11 '15 at 16:01
1

@ isn't special at all in JavaScript, other than the fact that it isn't valid in identifiers - as in you can't use it for function and variable names. You can't declare custom operators in javascript, so unless you get to speak to the one who built that site, there's no way to tell what they do with @ in strings.

On another note, many special characters are valid in JS, so you can name a function $, _ or even q̊̆̓̍u̐͂e̷̜r̤̻̫ͅy̎ if you want. But @ isn't one of them.

Domino
  • 6,314
  • 1
  • 32
  • 58