Sarasranglt
Sarasranglt

Reputation: 3879

libphonenumber-js in magento ( or with require js) returns undefined

Problem :

Some how this works fine when there's no require js :

https://jsfiddle.net/3xL745fu/

<script src="https://unpkg.com/[email protected]/bundle/libphonenumber-min.js">

</script>

<script>
  alert(new libphonenumber.AsYouType('US').input('213-373-4253'))
</script>

but with require js :

https://jsfiddle.net/vwu84jap/

<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
<script src="https://unpkg.com/[email protected]/bundle/libphonenumber-min.js">

</script>

<script>
  alert(new libphonenumber.AsYouType('US').input('213-373-4253'))
</script>

libphonenumber is undefined ?

Upvotes: 1

Views: 335

Answers (1)

Sarasranglt
Sarasranglt

Reputation: 3879

Solution is to load the js via require js:

<script src="https://cdnjs.cloudflare.com/ajax/libs/libphonenumber-js/1.7.18/libphonenumber-js.min.js">

</script>

<script>

   require([
       'libphonenumber'
   ], function (libphonenumber) {
       'use strict';

       console.log("libhphone number working");

       window.alert(new libphonenumber.AsYouType('US').input('213-373-4253'))

   });

</script>

https://jsfiddle.net/94jftb0e/

Upvotes: 2

Related Questions