I can't figure out why is not working a jquery plugin (base64 encode decode) inside an event function.
This way works:
$(document).ready(function(){
data = $.base64Encode('something');
alert(data);
});
But when trying to add in an event function, I get the $.base64Encode is not a function error
$(document).ready(function(){
$('#submit').click(function(e){
data = $.base64Encode('something');
alert(data);
e.preventDefault();
});
});
The Jquery plugin is found at: http://plugins.jquery.com/project/base64
In JavaScript there are two functions respectively for decoding and encoding Base64 strings: btoa() : creates a Base64-encoded ASCII string from a "string" of binary data ("btoa" should be read as "binary to ASCII"). atob() : decodes a Base64-encoded string("atob" should be read as "ASCII to binary").
These functions btoa and atob are deprecated for Node JS. If you are coding for the web browser you just need to prepend the window to get rid of this deprecation mark. Otherwise, you could install buffer with "yarn add buffer" or "npm i buffer" to run on browser.
To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.
Check that you're not including jQuery twice in the page. What this does is the first one loads, the plugin defines itself on that jQuery
object, and when jQuery is included a second time, the window.jQuery
object gets overridden...and the plugin won't be on it :)
You'd see this when running it later, whereas your document.ready
might be located before the 2nd jQuery inclusion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With