I am using laravel mix, which produces files correctly. Everything works fine, except when I call function form console I receive
Uncaught ReferenceError: test is not defined
at <anonymous>:1:1
This is part of code I get from running npm run dev. How can I call this function from console?
__webpack_require__(14);
function test() {
return 'testing';
}
$(document).ready(function () {
console.log(test());
});
Related
i have a file where i call functions collectively. This file contains the following code. i get error on non-library(jquery.barrating.min.js) pages running this code.
error i got "barrating is not a function error".
how can i get the code to run if only this function exists?
$(function() {
$('#example').barrating({
theme: 'fontawesome-stars'
});
});
If you want to run this code only if this function exists, you can test its presence in the jQuery prototype:
if (jQuery.prototype.barrating) {
// Your code goes here
}
I've tried delaying the code even if I execute window.functionName in the console before the code actually finishes it, I still get the same outcome (works when I directly enter window.functionName in the console but not when being executed from the javascript code).
Note this is all happening in the content-script of a chrome extension.
For full context, I have a Vue component which has a method that calls a function in another javascript file which eventually calls window.functionName -- so something like this:
SomeVueComponent.vue
<template>
<button #click="buttonClicked">
</template>
<script>
import { doSomething } from 'do-something.js';
export default {
...
methods: {
buttonClicked() {
doSomething();
}
}
}
</script>
do-something.js
export function doSomething() {
doFunctionName() {
window.functionName(); // Returns "Uncaught ReferenceError: functionName is not defined"
}
...
whenElementIsShown() {
// When the element was shown a script was injected to the page that sets window.functionName
// to something. I tried putting doFunctionName in a timeout and got the same results.
doFunctionName();
}
}
Whereas in the developer tools console...
window.functionName() // Returns "f (){...}"
I'm just not sure why window would be different in the two cases. Is it because of Vue? Is it some scoping issue? Is there a way to get the most recently updated window?
Turns out the window variable in the chrome extension is different than the page's window variable. More info here: Chrome extension - retrieving global variable from webpage
I have implemented a JS module which I use from several Node.js scripts.
I would like to be able to use this module also from a browser, and I would like all messages to be displayed in an HTML alert box instead of in the terminal.
Ideally, I would like to achieve this without changing my JS module (for example, by replacing all occurrences of console.log with alert, or by passing the logging function when I initialize the module).
So I've figured I could simply set console.log = alert right before I use this module from the client-side code that I have.
For all it matters, I am actually doing it with Electron (cross-platform desktop applications using JavaScript), but I have also tested it on a browser, and the result is the same.
With console.log = alert, I successfully change console.log from function log() {[native code]} into into function alert() {[native code]}.
However, when I then attempt to call console.log("test"), I get an exception TypeError: Illegal invocation.
Here is a simple client-side code which reproduces this problem:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
try{
alert(`before:\n- alert = ${alert}\n- console.log = ${console.log}`);
console.log = alert;
alert(`after:\n- alert = ${alert}\n- console.log = ${console.log}`);
console.log("test");
}
catch (error) {
alert(error);
}
</script>
</body>
</html>
Why am I getting this problem, and how I may resolve it (ideally without changing my JS module)?
Is it possible that alert eventually calls console.log, which yields some sort of infinite recursion?
The value of this matters.
alert expects this to be window not console.
Thus:
console.log = window.alert.bind(window);
Whenever I want to run a dataflow profile in Magento, the following happend:
the csv file is successfully loaded
Found X rows
The following line appears: Starting ... :: saveRow (handler-method)
But then an javascript-error occurs. For instance in Chrome Debugger you can see the details (starting from line 148):
function sendImportData(data) {
...
new Ajax.Request("http://www.magentoshop.com/index.php/admin/system_convert_profile/batchRun/", { ...
}
...
}
The error message is:
Uncaught TypeError: e is not a function
The used magento-version is 1.8.0.0
Just for your info.. We found the reason:
We used a minified version for prototype.js
We then changed this to the normal prototype-version and the ajax-request worked.
This is my code on the razor view engine
Delete
I have an external js file which contains the code for function detele:
alert('external');
function Delete(recordID, pID) {
alert('function called');
}
I added alert('external'); to check if I have the correct reference to the external js file, and it worked. Not sure why it keeps returning js error:
Microsoft JScript runtime error: 'Delete' is undefined