I have a module "./lib/common.js", like this:
function foo(text){
console.log(text);
}
function boo(text){
console.log(text);
}
module.exports=foo;
module.exports=boo;
I'm trying to include these functions in another js file using browserify :
var common =require('./lib/common.js');
$(document).ready(function() {
common.foo('hi');
});
Browserify creates the bundle,but on the browser I get
Uncaught TypeError: common.foo is not a function
Ok this was very stupid, I was overwriting my module.exports with the last row module.exports=boo;
With this change it works:
module.export.foo=foo;
module.export.boo=boo;
Related
I was trying to import a javascript file to another javascript file using jquery but I keep on running into the error $(document).ready(function () { ReferenceError: Cannot access '$' before initialization my current code is attached below. I included the $.getScript function inside $(document).ready(function()) { because I was trying to initialize jquery before $ assignment. Thanks in advance.
Current Code for trying to import js file
$(document).ready(function () {
//your code here
$.getScript("index.js", function(){
function time(){
alert("the time is" + h)
}
});
});
When I try to integrate the script type="module"... I get a reference error. My functions inside the "module" script are not found.
Im trying to integrate a printer into an existing webpage, the printer functionality is defined in the file "bpac.js". Its a javascript module using exports.
I have tried using this code in a sample website an everything worked fine. When I use my code in the existing file I get the error. When I dont use the "module" tag everything works fine, but then I cant use the functionality provided by "bpac.js". I have a simple button that when pressed, should execute some testfunction.
<script type = "module">
import * as bpac from './bpac.js';
window.testfunc = async function testfunc() {
const doc = bpac.IDocument;
const pName = await doc.GetPrinterName();
console.log(pName);
}
</script>
...
<button type="button" id="testen" onclick="testfunc()">test</button><br>
...
When I press the Button I get the Message: Uncaught ReferenceError: testfunc is not defined
at HTMLButtonElement.onclick ((index):1)
You are declaring function in wrong way. Use this.
window.testfunc = async function() {
const doc = bpac.IDocument;
const pName = await doc.GetPrinterName();
console.log(pName);
}
Why don't you use addEventListener to add as many listeners as you want?
document.getElementById("testen").addEventListener('click',function () {
console.log("Hello");
// Add your logic inside this function
});
In a Previous Commit:
We Call A Function like So var previousRoute = appRouter.getPreviousRoute();
Where appRouter is var appRouter = require("app_utilities/default/app-router");
and app-router contains an export as the following:
module.exports = {
getPreviousRoute: getPreviousRoute
}
function getPreviousRoute() {
return window.appPreviousRoute;
};
however in a latter commit the following line errors:
var previousRoute = appRouter.getPreviousRoute();
The Error would be: Uncaught TypeError: appRouter.getPreviousRoute is not a function
and we have to change it to:
var previousRoute = appRouter.getPreviousRoute;
I would like to know what would make it that we would need to drop the parentheses?
I have ran:
node -p process.versions.v8
6.8.275.32-node.36
Most likely as you have declared a variable not the type function to export and the variable inside export holds the reference of the function so if you directly access getPreviousRoute a function it will produce an error as you did not export a function and the program will not find that. So in terms of working when a variable is called the program will find that its declared then will look for its reference function that you gave and will execute it
Instead if you will export like
exports.getPreviousRoute = ()=>{}
It will not show you an error as its a type of function and will be accessible let me also point if i am wrong
I'm trying to get my head around require in simple web applications. I've got everything working fairly well until I try to execute some code inside a 'define' block;
define(['jquery'], function() {
var Playlist = function() {
// bunch of helper code here
};
return Playlist;
});
If i comment out the define wrapper the code works perfectly, but something I'm doing wrong here is causing 'Uncaught ReferenceError: Playlist is not defined'.
What am I missing about 'define' in AMD?
Try
define(['jquery'], function($) {
var Playlist = function() {
// bunch of helper code here
};
return Playlist;
});
And when will you get the Uncaught ReferenceError: Playlist is not defined error? Because Playlist is in your callback function and you can not use it outside of course.
I'm testing a js function that uses functions from other js files.
One of my external js files has a function defined as such:
functionname.functionextension = function () {.....}
when testing using jasmine, and calling functionname.functionextension, it complains that functionname is not defined. I think it believes that functionname is an object..
I know that one way to get around this is to modify the function name but I can't do that. Is there any other way?
Thanks
In javascript, all functions are objects. In the external js file, the function is probably defined like this:
var functionname = functionname || {};
functionname.functionextension = function () {
...
};
If you're getting a script error that functionname is not defined, there is either an error in the external javascript or you are not calling some initialization function that the external script requires to set up its objects.
It worked for me...u need to call function by its full name like functionname.functionextension() while calling.