Recently I found some interesting codes on a website.
<div class="inputArea">
<textarea type="text" id="textInput" class="chatInput lightBorder"></textarea>
</div>
<b>Send</b>
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?
Updated Answer
...now that you've shown what you think is "JavaScript code."
This isn't JavaScript code:
<b>Send</b>
<!-- 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 #.
# 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.
Related
I have a simple filter for searching images saved in a database. And therefore I use regex:Images.find({"name":{$regex:".*"+query+".*"}});
Of course I check the value with check(query, String); function. Could it be a big security issue, if I don't escape the special characters in the regex (query var, whose content is specified by user)? It is an advantage for me, that the users can define something like (nameOfImage1|nameOfImage2).
According to #Michel Floyd´s comment above, that is not a security problem, I use Regex in find(). But I also replaced some selected characters with query.replace(/[\/\\^$*[\]{}]/g, "");
I have some HTML tags which have ng-clicks and ng-ifs. Inside the respective expressions, I make function calls and pass in parameters, some of which are literals (mostly just true or false). So I would like to add a comment as to what a literal means, but angular doesn't seem to be able to parse this correctly. (I realize passing literals is not the brightest idea but I would nevertheless like to know the answer)
<button class='someclass' ng-click='somefunction(val1, val2, true /* explanation for literal */)' > </button>
How do I add comments in angular expressions?
No, comments are not supported. Parser sees / as an mathematical operator (see source code) which expects primary expression after it: e.g. something starting with (, or [, etc. However there is no valid expression in javascript that can include * immediately after / character. So parser throws an exception: Token '*' not a primary expression.
While the Angular documentation doesn't explicitly say that JavaScript comments are not supported. I would assume they are not.
Angular Expressions are only a subset of some JavaScript (and some added features like filters).
Why can't you pass these comments as separate parameters instead of appending other parameters?
Even though you achieve doing this somehow it would be not very good design.
I am trying to create my own javascript simple template function
I want to create a html page that will look like this
<p>
{{HELLO_WORLD}}
<br />
{{MY_NAME_IS}}
</p>
and than with javascript to replace anything that is in {{}}
with a json var that will look like this
{HELLO_WORLD: "Hello World!", MY_NAME_IS: "My name is"}
I am a little confused about the right method to do this.
the point is to make a multilanguage web site, that way I load the json for the desired language.
thank's.
JavaScript supports regular expression-based find-and-replace, with functions for the replacement. So you can do this:
myInputString.replace( /\{\{([^\}]*)\}\}/g, function( s, v ) { return myJSON[v] } );
To explain:
replace takes 2 arguments. The first is a regular expression object. In this case we build one inline using JavaScript's /expression/flags syntax. It looks for 2 opening braces (which need to be escaped because they have special meaning in regular expressions) followed by any characters which are not a closing brace, followed by 2 closing braces. The g means "global", so that it will match all cases rather than just the first one.
When a match is found, the function will be called. The first argument (I called it s) is the full matched string (like "{{abc}}"), the second (I called it v) is set to the first bit in brackets (like "abc").
In real code, you should add error checking (variables which don't exist), and possibly convert to lowercase / whatever.
Full details on replace are here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
I have a div that contains a settings icon that is a html miscellaneous symbol
<span class="settings-icon">⚙</span>
I have a jasmine test that checks the div contents to makes sure that it is not changed.
it("the settings div should contain ⚙", function() {
var settingsIconDiv = $('.settings-icon');
expect(settingsIconDiv.text())
.toContain('⚙');
});
It will not pass as it is evaluated as its glyph symbol of a gear icon ⚙
How to I decode the glyph in order to pass the test?
To get actual character from Unicode to compare it to a literal in HTML you can use String.fromCharCode() e.g.
.toContain(String.fromCharCode(9881))
You should check against the string '⚙' or, if you do not how to enter it in your code, the escape notation \u2699. There are other, clumsier ways to construct a string containing the character, but simplicity is best.
No matter how the character is written in HTML source code (e.g., as the reference ⚙), it appears in the DOM as the character itself, U+2699. In JavaScript, a string like ⚙ is just a sequence of seven Ascii characters (though you can pass it to a function that parses it as an HTML character reference, or you can assign it e.g. to the innerHTML property, causing HTML parsing, but this is rather pointless and confusing).
To match the browser behavior (because you don't know how it is encoded in html or in text) i would try the following
.toContain($("<span>⚙</span>").text()) instead of .toContain('⚙').
That way it should match how it is stored in the dom.
The String.fromCharCode(9881); mentioned by Yuriy Galanter will definitely also work reliable. But because dom engine and the js engine are two different parts, that could behave differently, i would test with both techniques.
I'm not talking about in the URL. I know what that does. I'm talking about how it's used in actual code.
After trying to assign it as a variable, I realized that it's reserved, but I don't know what for.
Javascript, or more precisely ECMAscript, is an evolving language. Some symbols and keywords (such as "class") have been reserved for future versions, even though they may not have any meaning at the moment.
I dont think, that this sign is somehow reserved fot another functionality. I found that rule here:
You must not use any punctuation marks of any kind in a JavaScript variable name, other than the underscore; for example... some:thing or big# or do'to would all be illegal.
This ist just, that javascript does not accept punctation signs in variable names ant due to this not parsing variables named like this as variables.
See here: What characters are valid for JavaScript variable names?
An ongoing proposal (currently in stage 4) utilizes hashtags to mark fields as private. It is part of the ES2022 standard.
Example:
class Foo {
x = 1; // public field
#y = 2; // private field
add() {
return this.x + this.#y;
}
}
In JavaScript variable names, no punctuation marks are permitted except for the underscore (_) and dollar sign ($). You can't start a variable name with a number, but otherwise all letters and numbers are permitted in a variable name.
So you can't have a variable name with # in it, no less a variable named #. It has no special meaning, it's just not permitted just as variable can't be named ~.