angular2 use external javascript file - javascript

I want to place my online game that I made that with pure javascript and html5 canvas in my angular2 project.
I wrote my scripts in external file and add the script tag in the head on angular index.html and in my component's typescript I declared my onload function like this:
declare var startGame1: any;
the startGame1 is a method that make the canvas ready for my game.
the problem is when I call startGame1() in AfterViewInit I get errors like this:
Unhandled Promise rejection: Error in :0:0 caused by: Cannot read property 'clearRect' of undefined ; Zone: angular ; Task: Promise.then ; Value:
ViewWrappedError {__zone_symbol__error: Error: Error in :0:0 caused by: Cannot read property 'clearRect' of undefined at ViewWrappedErro……} Error: Error in :0:0 caused by: Cannot read property 'clearRect' of undefined

I suggest you to load the external js using the .angular-cli.json.
In the apps -> scripts and insert the relative path of your external .js in the array.
And inside the app folder, create a .d.ts file to insert your declare var startGame1: any;. For example:
myGame.d.ts
declare var startGame1: any;
The myGame.d.ts will be loaded automatically by the webpack and then define your startGame1 for your app.

Related

How can I use a javascript function in index.html

I'm working on a react.js website where I'm supposed to call a javascript function in index.html. I'm experiencing some difficulties in calling the function.
This is what I'm doing in index.html
<head>
<script>
const getVar = getVarFromJs()
</script>
</head>
but whenever I do this, I get an error saying getVarFromJs is not defined. I also tried importing the function then I get an error saying "cannot use statement outside a module".
This is my js file:
let var = {};
export function getVarFromJs()
{return var;}
When I change the script type to module in index.html, I get this error:
Failed to load module script: Expected a Javascript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

Error in accessing JSON object in nodejs

I have a variable var sessions={} in file called 'UserSessions.js'. I am maintaining some information of each user in sessions={} with a unique timestamp. I have exported this variable to make it available to other files.
When I access the sessions={} in main file 'app.js' everything works fine.
But when I try to access same variable from another file 'Sessiongreet.js', it gives error.
Here is how I access the data :
Suppose '2017-04-07T11:55:40.162Z' is the unique timestamp assigned only once.
In app.js:
This Works fine:
const UserSessions=require('./UserSessions.js');
sessionId='2017-04-07T11:55:40.162Z';
var data=UserSessions.sessions[sessionId].context;
In sessionGreet.js:
This gives error:
const UserSessions=require('./UserSessions.js');
sessionId='2017-04-07T11:55:40.162Z';
var data=UserSessions.sessions[sessionId].context;
I know that UserSessions.sessions[sessionId].context exists as it is accessible in app.js file before accessing it in another file.
Here is the exact error what I get :
TypeError: Cannot read property '2017-04-07T11:55:40.162Z' of undefined
at initSession (/media/row_hammer/sessionGreet.js:24:33)
at Object.run (/media/row_hammer/sessionGreet.js:67:2)
at Object.handlePostback (/media/row_hammer/sessionTemp.js:89:19)
at runPostback (/media/row_hammer/app.js:113:15)
at /media/row_hammer/app.js:161:3
at Object.findOrCreateSession (/media/row_hammer/UserSessions.js:83:4)
at Bot.bot.on (/media/row_hammer/app.js:159:15)
at emitThree (events.js:116:13)
at Bot.emit (events.js:194:7)
at Bot._handleEvent (/media/row_hammer/node_modules/messenger-bot/index.js:254:10)
Also, In sessionGreet.js:
//EVEN this line shows 'undefined'
console.log(UserSessions.sessions);
Why am I getting this error even though flow of program is correct?
It may be that app.js and Sessiongreet.js are in different folders.
'./UserSessions.js' means that UserSessions.js is in the same folder as the file issuing the require().
If app.js is in the same folder as UserSessions.js, but Sessiongreet.js is in different folder, this would explain your issue.

Issues Integrating ACE Editor with Keystonejs App

It says here(http://ace.c9.io/#nav=embedding) just copy one of src* subdirectories somewhere into your project
I have put it in mykeystoneapp/public/js(my default home is mykeystoneapp/public)
Here are the errors I get:
1.Uncaught TypeError: $.cookie is not a function(ui.js:8)
2.Uncaught Error: Missed anonymous define() module: function …(require.js:141)
http://requirejs.org/docs/errors.html#mismatch
Here is my Jade code:
script(src='/js/ace/demo/kitchen-sink/require.js')
script.
require.config({paths: {ace: "/js/ace/build/src"}});
define('testace', ['ace/ace'],
function(ace, langtools) {
console.log("This is the testace module");
var editor = ace.edit("editor_container");
editor.setTheme('eclipse');
editor.session.setMode('javascript');
require(["/js/ace/lib/ace/requirejs/text!src/ace"], function(e){
editor.setValue(e);
})
});
require(['testace']);
Secondly if I put debugger in EventEmitter(https://github.com/ajaxorg/ace-builds/blob/master/src/ace.js#L3300)
I can see it’s properly reaching EventEmitter._dispatchEvent with
eventName=‘changeMode’ but it returns without any operation as there are no !listeners or defaultHandler
editor.session.setMode('javascript'); is wrong, it should be editor.session.setMode('ace/mode/javascript'); instead. Same for theme which is supposed to be ace/theme/eclipse.
error in ui.js is not related to ace, since ace doesn't have a file named ui.

JCIFS Package Reference Error in Javascript

I am trying to use JCIFS to access file in a shared folder and it works perfectly fine using a java program. But when I try to use this code in a javascript which is embedded in a ant script file I get the below error. I do see the jcifs-1.3.17.jar in the classpath of ANT but still it is complaining of reference error (ReferenceError: "jcifs" is not defined.).
Appreciate your inputs/thoughts on this. Thanks in Advance!!!
2014-07-17 11:41:32,739 ERROR org.apache.bsf.BSFManager - Exception :
java.security.PrivilegedActionException: org.apache.bsf.BSFException: JavaScript Error: Internal Error: org.mozilla.javascript.EcmaError: **ReferenceError: "jcifs" is not defined.**
at java.security.AccessController.doPrivileged(AccessController.java:255)
at org.apache.bsf.BSFManager.exec(BSFManager.java:491)
at org.apache.tools.ant.util.optional.ScriptRunner.executeScript(ScriptRunner.java:100)
at org.apache.tools.ant.taskdefs.optional.Script.execute(Script.java:52)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:600)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: org.apache.bsf.BSFException: JavaScript Error: Internal Error: org.mozilla.javascript.EcmaError: ReferenceError: "jcifs" is not defined.
at org.apache.bsf.engines.javascript.JavaScriptEngine.handleError(JavaScriptEngine.java:195)
at org.apache.bsf.engines.javascript.JavaScriptEngine.eval(JavaScriptEngine.java:147)
at org.apache.bsf.util.BSFEngineImpl.exec(BSFEngineImpl.java:141)
at org.apache.bsf.BSFManager$6.run(BSFManager.java:493)
at java.security.AccessController.doPrivileged(AccessController.java:251)
... 19 more
Need to preface the package name with Packages.
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java#External_Packages_and_Classes

Why is the first api.add_files not being included?

I have the following:
scribe/package.js:
Package.describe({
summary: 'A rich text editor framework for the web platform http://guardian.github.io/scribe/'
});
Package.on_use(function(api) {
api.add_files('require.js', 'client');
api.add_files('scribe.js', 'client');
});
For some reason I'm getting this error:
Uncaught ReferenceError: define is not defined
Because require.js doesn't show up in the rendered page. Why is that?
File tree:
You need to export all the top global variables from the package. Add this line before the api.add_filescall:
api.export('define')
Add a similar line for every global variable you'd like to be abke to use outside of the package.

Categories

Resources