Dynamically insert special character into document title - javascript

I am trying to update the document.title with Javascript. This works fine, but now I want to insert the ü character into the title, which gives me a question mark at the place where the ü is supposed to be.
Here is my code (I omitted the rest of the title for clarity):
document.title = '\u2019 - \u252';
The strange thing is that the \u2019 part does work and gives me a quote, as expected. My charset is UTF-8, by the way.
What am I doing wrong here?

Do you want document.title = '\u00FC'; ? the &# and \u numbers are different in many places, or so i've found in my usages.

What's \u252? It doesn't seem to be a valid unicode escape, try \u0252?

Update: Apologies. I read it wrong. Thought you were looking for "ɒ" which is the result I get...
This worked for me on Chrome, Firefox and Safari (OSX)
document.title = '\u0252';

Related

How to filter emojis from string jquery/javascript?

I'm using the following to exclude emojis/emoticons from a string in php. How do I do the same with javascript or jQuery?
preg_replace('/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u', '', $text);
This is what I try to do
$('#edit.popup .btn.save').live('click',function(e) {
var item_id = $(this).attr('id');
var edited_text = $('#edit.popup textarea').val().replace(/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u, '');
$('#grid li.image#' + item_id + ' img').attr('data-text', edited_text);
});
I found this suggestion in another post on Stack Overflow, but it's not working. It's still allowing emojis from ex ios.
.replace(/([\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])/g, '')
What I try to achieve is to not allow emojis in textfield, and if an emoji is inserted (from ex ios keyboard) it will be replaced by nothing. It works with php. Someone here who can help me out with this?
Based on the answer from mb21, this regex did the job. No loop required!
/[\uD800-\uDBFF][\uDC00-\uDFFF]/g
As pointed out in this answer, JavaScript doesn't support Unicode code points outside the Basic Multilingual Plane (where iOS emojis lie).
I highly recommend reading The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!). Then you'll understand what was meant with:
So some indirect approach is needed. Cf. to JavaScript strings outside of the BMP.
For example, you could look for code points in the range [\uD800-\uDBFF] (high surrogates), and when you find one, check that the next code point in the string is in the range [\uDC00-\uDFFF] (if not, there is a serious data error), interpret the two as a Unicode character, and replace them by whatever you wish to put there. This looks like a job for a simple loop through the string, rather than a regular expression.

Wordpress & Javascript: String variable having html tags being read by browser with newline character

I have gone crazy trying to resolve this issue.
In my javascript code I have am defining a string variable in which I am putting an HTML table in the form of string.. i.e.:
var tData="<table><tbody><tr><a><th>Type</th><th>Score</th><th>Percentile</th></a></tr><tr><td><a>Overall</a></td><td>2.4</td><td>50%</td></tr><tr><td><a>Best 100</a></td><td>2.3</td><td>70%</td></tr></tbody></table>";
Now this variable assignment through the string is being read by my browser (both chrome and firefox) as an HTML code with line breaks. Take a look at the image below for more clarity.
The code works fine if I remove html tags and write a simple string. So I can assure you there are no previous inverted comma errors (i checked them multiple times) and no bogus characters.
I have spent too many hours on this issue. Please please help me on this.
EDIT
Added Wordpress in title and Tags as this is a wordpress issue.
Since your document is XHTML, you have to enclose your code into a CDATA section:
<script>
<![CDATA[
// code here
]]>
</script>
This prevents the browser from interpreting <...> sequences in the content as tags.
If you want multiline strings in JavaScript, you have to unescape the newline, ie
var str = "abc\
de";
Ok. Eureka!!!
I found a get around. I broke the following string :
var tData="<table><tbody><tr><a><th>Type</th><th>Score</th><th>Percentile</th></a></tr><tr><td><a>Overall</a></td><td>2.4</td><td>50%</td></tr><tr><td><a>Best 100</a></td><td>2.3</td><td>70%</td></tr></tbody></table>";
into
var tData = "<tab"+"le><tb"+"ody><t"+"r><a><t"+"h>Type</t"+"h><t"+"h>Score</t"+"h><t"+"h>Percentile</t"+"h></a></t"+"r><t"+"r><t"+"d><a>Overall</a></t"+"d><t"+"d>2.4</t"+"d><t"+"d>50%</t"+"d></t"+"r><t"+"r><t"+"d><a>Best 100</a></t"+"d><t"+"d>2.3</t"+"d><t"+"d>70%</t"+"d></t"+"r></tbo"+"dy></ta"+"ble>";
to fool the browser. I am still hoping for a better answer please.
Delete all invisible characters (whitespace) around that area,
then give it another try.
Try this:
var tData="<table><tbody>";
tData+="<tr><th><a>Type</a></th><th>Score</th><th>Percentile</th></tr>";
tData+="<tr><td><a>Overall</a></td><td>2.4</td><td>50%</td></tr>";
tData+="<tr><td><a>Best 100</a></td><td>2.3</td><td>70%</td></tr>";
tData+="</tbody></table>";
Possible Duplicate No visible cause for "Unexpected token ILLEGAL"

How to match non-ASCII (German, Spanish, etc.) letters in regex?

I was unable to find or create a regex which match only letters,spaces, accented letters and spanish and german letters.
I'm using this for now:
var reg = new RegExp("^[a-z _]*$");
I've tried:
^[:alpha: _]*$
^[a-zA-Z0-9äöüÄÖÜ]*$
^[-\p{L}]*$
Any idea? Or the regex supported by javascript engines are limited?
The 2nd to last case looks like it should work, but is missing a " " and "_":
/^[a-zA-Z0-9äöüÄÖÜ]*$/.test("aäöüÄÖÜz") => true in FF 3.6 and IE8
/^[a-zA-Z0-9äöüÄÖÜ]*$/.test("é") => false in FF 3.6 and IE8
I'm am unable to find the other constructs in the ECMAScript specification.
Happy coding.
Edit Also check the page encoding and make sure it is "unicode" (UTF-8 likely). If this can't be ensured, then use the \uXXXX escape sequences in the regular expression (using the escapes can be done anyway and may help with source code editing/control).
I'm parsing a name input field, and this seems to be working for both German and French:
^[a-zA-Z\-ÀàÂâÆæÇçÈèÉéÊêËëÎîÏïÔôŒœÙùÛûÜü]*$
Some folks have names like 'Rölf-Dieter', and this lets them through, while checking for numbers. A little extreme, but it works!

Newline \n problem in JS

I am reading a file with xmlHttp object and splitting the responseText with newlines by split method.
But "\n" character literally doesn't work. It acts like an error in my code and causes my code not even function.
Here is the line:
var lines=myPlaylist.responseText.split("\n");
There is no error if I split the array myPlaylist with other characters.
Just \n causes problem which I fail to understand.
At first, I thought the error was due to white-space:nowrap since I execute my code on Chrome.
Though I never used white-space in anywhere, I tried to set it to normal but it didn't work.
Similarly, I tried my code on other browsers (Firefox, IE etc), it didn't work either. Looks like I have a problem with using \n. Is there any other way to use newline or error with my code?
And by the way, error seems to be a syntax error since it does not just ignore \n character. Simply causes my code not to work
EDIT: An example responseText
[playlist]
File1=http://localhost:7000/Videos/Big%20Buck%20Bunny%20Trailer.ogv
Title1=Bunny Trailer
Length1=23
File2=http://localhost:7000/Videos/Dizzy%20Cat%20Video.ogv
Title2=Kedi
Length2=33
NumberOfEntries=2
Version=2
I found my own solution to my problem.
After using random special characters, \r character used for carriage return worked like a charm for my problem.
It acted like a newline \n character or at least it did its job in my case.
Thanks everyone for answers and helpful comments.
Try using this line
/\n/
Instead of this one
"\n"
Here's an SO thread that will provide a bit more insight
JavaScript string newline character?
EDIT1:
I just tested this and it splits appropriately on new lines. Can you post some of what you're trying to split?
<html>
<script>
function testSplit(value)
{
var lines = value.split(/\n/);
alert(lines);
}
</script>
<body>
<textarea id="test" name="test" onblur="testSplit(this.value);">
</textarea>
</body>
</html>
EDIT2:
Can you try converting your responseText to an object and seeing what you get from it - sorry, just shooting from the hip here since I haven't had time to mock up anything for testing.
eval("var playlistResponse = ("+ myPlaylist.responseText +")");
Here's a somewhat old article that might be useful to you: http://www.peachpit.com/articles/article.aspx?p=443580&seqNum=4
Use \\n instead of \n i tried on my code and it is working fine
This should work without problem.
Are you certain that myPlaylist has a responseText property, and that property is a string?
What happens if you catch an eventual error?
try {
var lines = myPlaylist.responseText.split(/\n/g);
alert(lines.length);
} catch (e) {
alert(e.message);
}

JQuery: How to text insert HTML ascii character?

I have the following JQuery code:
$(this).text('Options ▴');
However, the ascii code isn't showing up on my web page. All that shows up is on the webpage is Options ▴.
What am I doing wrong?
Use the .html() method instead of .text().
The whole point of .text() method is to make the text appear exactly as it's in the string, with all tags and entities.
If you use a unicode escape ('\u25B2') instead of the html escape character,
you can use the .text method.
Some users will not have a font that will display either version.
&#x25B4 is definitely not ascii (ascii's a character code that runs from 0 to 127...!-) -- it seems to be a high-page unicode character. What char encoding is your page using? With something "universal" like utf-8, unicode characters should show up... if the browser has them in its font, of course. With other encodings, such characters might just be impossible to transmit and show.
..or if someone wants to use just javascript:
var d = document.createElement("div");
d.appendChild(document.createTextNode("500"));
var euro = document.createElement("span");
euro.innerHTML = "€"; //your character
d.appendChild(euro);
prints: 500€
You can try this:
$(this).html('Options ▴');
Output:
Options ▴
If you want to test it online you can check out this website. (I liked it very much)

Categories

Resources