Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery base64 function implementation

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

like image 525
Pentium10 Avatar asked Aug 21 '10 19:08

Pentium10


People also ask

How do you do base64 encode in JS?

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").

Is BTOA deprecated?

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.

How do I decode a base 64 string?

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.


1 Answers

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.jQueryobject 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.

like image 138
Nick Craver Avatar answered Sep 24 '22 21:09

Nick Craver