Javascript library not working on Device - javascript

I am using the BigNumber library of MikeMcl to handle my needs for big numbers. I use this library in an Ionic/Angular project.
As explained on Github, the way to install and use this library is to include a script tage in your html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<!-- compiled css output -->
<link href="css/ionic.app.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
<script src="cordova.js"></script>
<!-- ********* BIGNUMBER library ********* -->
<script src='lib/bignumber.js/bignumber.min.js'></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/services.js"></script>
</head>
<body ng-app="starter">
<ion-nav-view></ion-nav-view>
</body>
</html>
Now in my code, I can use this library as for instance:
x = new BigNumber(123.4567)
y = BigNumber('123456.7e-3')
z = new BigNumber(x)
x.equals(y) && y.equals(z) && x.equals(z) // true
I tested this and it works fine on Chrome and on Safari (even on device).
But when I install the app on my phone, using Phonegap Build, the app does not work anymore (I checked that this library is the cause by removing the BigNumber syntaxes from my code). Moreover, the Angular in the app just crashes and shows for instance {{variableName}} and nothing is working.
Because I develop in a cloud environment, I did try to debug the app using the Safari Developer Debug Console on a mac (by plugging my phone with an USB to the mac and then enabling Developer tools in Safari).
However, no errors were found with exception of one:
file not found: "path/to/ionic/lib/js/angular.min.js.map" 404
But this is not the cause of the problem.
What is going on? How can I still use this javascript library on my device?

I have just tested your code and it doesn't work because when you create the y variable you don't use the word new.
This is the code I've used and it works
x = new BigNumber(123.4567)
y = new BigNumber('123456.7e-3')
z = new BigNumber(x)
alert( x.equals(y) && y.equals(z) && x.equals(z));// I get true

#JohnAndrews,
If this is the extent of your code, it is missing the deviceready listener. On cordova/phonegap you typically cannot do stuff, until you get the deviceready event. So, this is the only thing I can think of, Can you add a function onDeviceReady() {} and see if that fixes your problem?
Oh and, of course, make sure you run your operations AFTER this event.

Found the solution. If you are also experiencing that a javascript library is not working on your device, but it does in the browser, then this answer might help you.
The problem occured that when I ran npm install to install the package, that it did not update my .git dependencies properly. What happened is that every time I packaged my app through Github and Phonegap Build, the folder of BigNumber was not taken into account. So basically, the file bignumber.js was not available in the app and thats why it did not work.
I solved it by copying all the files from the BigNumber folder to a new custom folder.
A related question is here: Git not pushing all files and folders

Related

React boilerplate doesn't load js files in the index.html

I'm testing React-boilerplate and I'm trying to load some javascript files in the app/index.html file:
<!doctype html>
<html lang="en">
<head>
<!-- The first thing in any HTML file should be the charset -->
<meta charset="utf-8">
<!-- Make the page mobile compatible -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Allow installing the app to the homescreen -->
<link rel="manifest" href="manifest.json">
<meta name="mobile-web-app-capable" content="yes">
<script type="text/javascript" src="myJScriptFile1.js"></script>
<script type="text/javascript" src="myJScriptFile2.js"></script>
</head>
<body>
<!-- Display a message if JS has been disabled on the browser. -->
<noscript>If you're seeing this message, that means <strong>JavaScript has been disabled on your browser</strong>, please <strong>enable JS</strong> to make this app work.</noscript>
<!-- The app hooks into this div -->
<div id="app"></div>
<!-- A lot of magic happens in this file. HtmlWebpackPlugin automatically includes all assets (e.g. bundle.js, main.css) with the correct HTML tags, which is why they are missing in this HTML file. Don't add any assets here! (Check out webpackconfig.js if you want to know more) -->
</body>
</html>
This is the default index.html file. I'm running it with npm or yarn and the server console shows me no error, but my browser console keeps telling me:
Uncaught SyntaxError: Unexpected token <
Just like this, there is no more error. These javascript files work fine because I'm using them into another React based project, and I'm calling them into the index.html file as well. The console prints that error per file. If I trace the error it leads me to the correspond .js file.
After almost a week searching the web I couldn't find a solution. So that's it, does anybody have any idea on how to solve this?
Well, I'm answering myself. I needed a little bit more of research in webpack. I achieve this through webpack:
installed npm i add-asset-html-webpack-plugin -D
then, in the webpack config file, under plugins I added:
new AddAssetHtmlPlugin({ filepath: require.resolve('./some-file') })
and that's it.

Aurelia on Heroku, JSPM's system.js "blocked due to MIME type mismatch"

I'm trying to host a simple (Typescript) Aurelia app on Heroku with a very simple Express server, but when I spin it up I get the following error in Firefox:
The resource from
“https://ub-edis.herokuapp.com/jspm_packages/system.js” was blocked
due to MIME type mismatch (X-Content-Type-Options: nosniff).
and then all sorts of subsequent errors because the code obviously can't resolve System.
Running this code locally works perfectly well. Since I'm not sure how to configure Heroku to install my JSPM and Typings dependencies, I'm using dropbox to upload my repo. So I just copy and pasted my whole project directory over (I know this isn't ideal, but I'm just a newby trying to get this thing to work with minimal effort, not to produce production code). If I look under the Sources tab in the Chrome dev tools it finds my config.js correctly, so I don't think I'm getting the error because it can't find system.js.
This is my index.html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>EDIS</title>
</head>
<body aurelia-app="main">
<h1>Loading...</h1>
<script src="jspm_packages/system.js" type="text/javascript"></script>
<script src="config.js" type="text/javascript"></script>
<script type="text/javascript">
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
I'm a little out of my depth with this, so any help would be greatly appreciated!

How to setup the environment for Angular 2.0?

Taking my initial steps towards Angular 2.0.
First thing is to set up the right environment for the development.
My index.html
<!DOCTYPE HTML>
<html>
<head>
<title>Welcome to Angular 2.0</title>
<!--css-->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
</head>
<body>
<div class="container">
<h1> Hello Angular 2 </h1>
<my-app> Loading app component....<my-app>
</div>
<!--js-->
<!-- Polyfills for older browsers -->
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js#0.7.4?main=browser"></script>
<script src="https://unpkg.com/reflect-metadata#0.1.8"></script>
<script src="https://unpkg.com/systemjs#0.19.39/dist/system.src.js"> </script>
<script> window.autoBootstrap = true; </script>
<script src="https://cdn.rawgit.com/angular/angular.io/b3c65a9/public/docs/_examples/_boilerplate/systemjs.config.web.js"></script>
<script>
System.import('app').catch(function (e) { console.log(e); });
</script>
<!--js-->
</body>
</html>
I have copied the content from https://angular.io/docs/ts/latest/guide/setup.html for the following files:-
app.component.ts
app.module.ts
main.ts
NPM is installed & running.
But in order to get the node_modules folders with the required dependencies for the app to run.
What commands do I need to run.
What are the commands that I need to run to set up the Angular 2 environment?
Please note I am new to NPM.
Thanks.
You have angular-cli wich is a good starter point. It will set you everything up according to best practices.
Simple way to set up your first Angular 2 Application.
Download the zip version here.firstAngular2App
Extract it to your destination folder. Assume D:
Open your command prompt(ensure that all required softwares are installed).
Navigate it to the folder. Use the command cd D:\firstAngular2App
Executre npm install
Once it is done use npm start
This way you have your first Angular2 appliction up and running.
npm install in the root project (it will create your node_modules from the dependencies mentioned in package.json file, should have those files as a start)
If you are already using Visual Studio Code as an IDE, you may want to explore how they recommend setting up Angular. I personally found their installation guide painless and feature-rich. I have never walked through an official Angular installation before due to being drenched in jQuery projects. This guide was simple enough to give me a good beginners understanding.

How to enable minified JavaScript files in Play 2.3.1 templates?

I was able to load the sbt-uglify 1.0.3 plugin in my Play Framework 2.3.1 app. Loading of the non-minified javascripts is pretty straightforward, but loading the minified versions seems to be impossible.
In my template I use <script> tags similar to this:
<script src="#routes.Assets.at("javascripts/app.js")"></script>
In dev mode, the non-minified javascript version is loaded, which is fine. In prod mode (using activator start) I see sbt-uglify generating the minified versions to the target/web/uglify/build folder, but because I didn't change the above <script> tag line in my templates, the non-minified versions of the javascripts files are loaded.
Is there a way to do a prod-only mapping of such routes to load the minified versions?
The issue Reverse Router should use minified assets in production automatically was fixed in Play 2.3.1 that exactly matches your requirement.
According to Play 2.3.1 Changelog:
The behaviour of the Assets reverse router has changed, if minified
versions of assets exist, it now returns a URL for those instead. To
disable this behaviour, set assets.checkForMinified=true in
application.conf.
NOTE It should rather read set assets.checkForMinified=false, but anyway...
What follows works in production mode only so start the application with activator start not run or use the generated start scripts (after stage).
The behaviour of using minified versions of assets in production should be enabled by default in the version of Play with #routes.Assets.versioned (not routes.Assets.at).
It does require that the appropriate route declaration in conf/routes is:
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
What I found a bit unclear at first was the order of elements in pipelineStages as well as the requirement to include sbt-rjs in it.
And just after I'd written the sentence about the order I found in the section "RequireJS" in Play 2.3 Migration Guide:
The order of stages is significant. You first want to optimize the
files, produce digests of them and then produce gzip versions of all
resultant assets.
I've also found in Play 2.3 Migration Guide in the section "Closure Compiler":
UglifyJS 2 is presently provided via the RequireJS plugin (described
next). The intent in future is to provide a standalone UglifyJS 2
plugin also for situations where RequireJS is not used.
It all started with the answer to Play 2.3 sbt-web plugin Javascript minification.
So, the below pipelineStages is the working one - mind the order and rjs:
pipelineStages := Seq(rjs, uglify, digest, gzip)
project/plugins.sbt used was as follows:
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.5")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-uglify" % "1.0.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-gzip" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.6")
Don't forget to create an empty app/assets/javascripts/main.js file to let sbt-rjs do its job.
As a test, I created a Play application with activator new playApp play-scala and applied the above changes in the build as well as in app/views/main.scala.html that ultimately looked as follows (note #routes.Assets.versioned):
#(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>#title</title>
<link rel="stylesheet" media="screen" href="#routes.Assets.versioned("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="#routes.Assets.versioned("images/favicon.png")">
<script src="#routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script>
</head>
<body>
#content
</body>
</html>
Executing activator start and calling curl http://localhost:9000 gives (formatting's mine for the sake of readability):
➜ play-uglify curl http://localhost:9000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Play</title>
<link rel="stylesheet" media="screen" href="/assets/stylesheets/d41d8cd98f00b204e9800998ecf8427e-main.css">
<link rel="shortcut icon" type="image/png" href="/assets/images/84a01dc6c53f0d2a58a2f7ff9e17a294-favicon.png">
<script src="/assets/javascripts/4302136334616ae0605d47a1932ee262-hello.min.js" type="text/javascript"></script>
</head>
<body>
<h1>Your new application is ready.</h1>
</body>
</html>
Note 4302136334616ae0605d47a1932ee262-hello.min.js and the non-JavaScript resources digested.

Spine.js mobile (phonegap) ReferenceError Jqueryify

My problem is very much like the Hem on windows problem: Uncaught module jqueryify not found
I can't deploy my spine mobile app to a android mobile devise using phonegap, it works perfectly in a browser (linux) but whenever I run it through eclipse on a android devise (various versions tested) I recieve:
06-17 18:39:36.878: E/Web Console(5976): ReferenceError: Can't find variable: require at file:///android_asset/www/index.html:9
Which is referencing:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>App</title>
<link rel="stylesheet" href="/application.css" type="text/css" charset="utf-8">
<script src="/application.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var jQuery = require("jqueryify"); //REFERENCED LINE 9
var exports = this;
jQuery(function(){
var App = require("index");
exports.app = new App({el: $("body")});
});
</script>
</head>
<body>
</body>
</html>
I have installed the dependencies through npm and also built it using Hem, I'm kind of lost to what the problem might be.
Any ideas why this might be happening?
** EDIT **
The problem was the .js file was not being found, needed:
<script src="./application.js" type="text/javascript" charset="utf-8"></script>
NOTICE THE '.' in the javascript file src att.
The error specifically means that the require function is undefined when it executes your inline script. Since require isn't a built-in part of browser JavaScript, that means whatever code (presumably a module-loader library) should be providing it either isn't getting executed or is failing. Given your code, that would have to be somewhere in application.js.

Categories

Resources