67675

How to make my own string in javascript as if I hit ALT codes on keyboard (UTF-8)

I am trying to create some random unicode strings within javascript and was wondering if there was an easy way. I tried doing something like this...

var username = "David Perry" + "/u4589";

But it just appends /u4589 to the end which is to be expected since it's just a string. What I WANT it to do is convert that into the unicode character in the string (AS IF I typed ALT 4589 on the keypad). I'm trying to build the string within javascript because I wanna test my form with various symbols and stuff and I'm tired of trying ALT codes to see what weird characters there are... so I thought.. I would loop through ALL unicode characters for FUN and populate my form and submit it automatically...

I was going to start at /u0000 and go up to /uffff and see which codes break my website when outputting them :)

I know there are different functions in JS but I can't seem to figure out why I can't build a string of unicode characters. lol.

If it's too complicated don't worry about it. It's just something I wanted to tinker with.

Answer1:

Try "\u4589" instead of "/u4589":

>>> "/u4589" "/u4589" >>> "\u4589" "䖉"

the forward slash (/) is just a forward slash in a string, however the backslash (\) is an escape character.

Answer2:

If you wish to generate random characters or loop through a range of characters, then you could use String.fromCharCode(), which gives you the character with the Unicode number passed as argument, e.g. String.fromCharCode(0x4589) or String.fromCharCode(i) where i is a variable with an integer value.

Both the \uxxxx notation and the String.fromCharCode() work up to 0xFFFF only, i.e. for Basic Multilingual Plane characters. This may well suffice, but if you need non-BMP characters, check out e.g. the MDN page on fromCharCode.

Recommend

  • Scheduling function calls in a Python curses UI
  • A way to redirect keypresses from TWebBrowser to the ParentForm
  • “Next” button on Android Numeric keypad
  • Objective C Textfield Default Value “•••••••” to \"1•••••••”,“12•••••”,“123••••” when user cli
  • How can I default to the numeric keypad on focus of an EditText view?
  • numberOfRowsInSection called but cellForRowAtIndexPath not called
  • How do I remove duplicates from an AutoHotkey array?
  • How to cache images only in disk using Kingfisher?
  • Greek letters in a GUI - PYTHON
  • Why do native C++ projects have a TargetFrameworkVersion?
  • CakePHP Subquery from SQL
  • Vim ctags behaves strangely
  • How to subscribe to an event without preventing garbage collection?
  • Objective-C : getting error on console while trying to display app on ipad device?
  • Android libgdx prefs getting lost
  • Android Oreo JobIntentService Keep running in background for Android 7 &below and crashing often
  • Multilingual set up of codeigniter
  • Is there an HTML code that can make my background picture transparent and my text non-transparent?
  • Get textarea in codebehind
  • Sympy: working with equalities manually
  • Connect Node.js with Oracle on Windows platform
  • remove unicode characters but keep all special and English characters with preg_replace
  • Possible to “watch” both HAML and SASS at the same time?
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • JSON with duplicate key names losing information when parsed
  • Convert array of 8 bytes to signed long in C++
  • jQuery tmpl and DataLink beta
  • Jquery - Jquery Wysiwyg return html as a string
  • XCode can't find symbols for a specific iOS library/framework project
  • SQL merge duplicate rows and join values that are different
  • Proper way to use connect-multiparty with express.js?
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Net Present Value in Excel for Grouped Recurring CF