Transform alert to console.log [duplicate] - javascript

This question already has answers here:
JavaScript: Overriding alert()
(12 answers)
Closed 4 years ago.
Is it possible to change the alert() function to log the message instead of showing up a popup?
Some outside team members does like the use the alert function, but we don't want it because they aren't removing them.
So I want to add some code in our angular application that logs the message in the console instead of popping up.
So, I want some code which don't require to change all the calls to alert() but a kind of hook to that function which transforms it to console.log()

Try this
window.alert = window.console.log
window.alert('Hello from console.log')

Related

Javascript, Stop function from logging to console on call [duplicate]

This question already has answers here:
How to quickly and conveniently disable all console.log statements in my code?
(38 answers)
Closed 1 year ago.
Say I have a function imported from a library
import doSomething from 'someLibrary';
And doSomething has a console.log() call inside of it. So, when I call do something
doSomething();
Some message is logged to the console. How do I call the function doSomething without it logging anything to the console?
Try this:
{
let console = {log: ()=>{}};
doSomething();
}

How to unset chrome.windows.onRemoved.addListener? [duplicate]

This question already has an answer here:
`chrome.webRequest.onBeforeRequest.removeListener`? -- How to stop a chrome web listener
(1 answer)
Closed 6 years ago.
I have the following code in my Chrome extension to detect when windows are closed:
closeListener = chrome.windows.onRemoved.addListener(function(closed_window_id){
// something
}
How do I unset this such that the anonymous function does not fire? i.e. Something like:
chrome.windows.onRemoved.removeListener(closeListener)
ANSWER
Stephan/wOxxOms answer is correct. The function within the addListener cannot be anonymous and the removeListener syntax uses the function name (or a variable pointing to the function) to clear it.
Updated codepen: http://codepen.io/anon/pen/EgpNpz
After taking a look at your code, I see your problem. The function you put into the addListener is anonymous and needs to be set to a variable or become a named function.
function newListener() {
alert();
}
//This will add the listener
chrome.windows.onRemoved.addListener(newListener);
//This will remove it
chrome.windows.onRemoved.removeListener(newListener);

Error in Overriding alert with console.log function in javascript [duplicate]

This question already has answers here:
Uncaught TypeError: Illegal invocation in JavaScript
(2 answers)
Closed 6 years ago.
I am new to javascript. Due to certain reasons, I need to override windows.alert function by console.log function. For that, I have written following code,
window.alert = console.log;
alert('abc'); // gives TypeError : Illegal invocation.
I don't know what's wrong I am doing here. As per my knowledge, it's possible to modify javascript function reference with another function object.
Edit
Many have downvoted my question and given me reference to similar question, but
my problem is different. I want to suppress the warning of datatable grid library in jquery. That's why I want to replace alert by console.log or other function.
Hope this should work...
alert("msg");
function alert(msg){
console.log(msg);
}
This should do
var alert = (...params) => console.log(...params);

rules of using javascript GOTO , LABEL etc [duplicate]

This question already has answers here:
How can I use goto in Javascript?
(16 answers)
Closed 9 years ago.
Can anybody help me in understanding the use of goto , label etc in javascript, I have a program where I need to jump directly to a particular block of code...I am trying to use "break labelName" , but its showing an error that "labelName not found". Is there any rule to use label with break or continue
Thanks in advance .
You must be new to javaScript ;)
Anyhow: defining a function is defining your named excess point:
function doSomething(someVariable){
alert(someVariable);
}
So, somewhere else in your code you can now call doSomething('Hello world!');
Calling the function as above is the GOTO part and doSomething correlates to LABEL. As you see you can also pass some parameters (here it's someVariable) to the function (the executing code block) and work with it (like here the alert that causes an alert box showing in your browser showing the assigned value 'Hello world!'.
Maybe this also helps you: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function or http://www.w3schools.com/js/js_functions.asp

Bringing back an "overridden" window method [duplicate]

This question already has an answer here:
Call native javascript function that has been "erased" by the web page
(1 answer)
Closed 4 years ago.
If I reset window.alert to be some other function, is there any way for me to restore it w/o first "saving" it?
For example:
window.alert = function() { };
After doing that, is there a way of restoring window.alert() to what it used to be? (btw: asking this only for "predefined" DHTML objects - not for random js objects)
You can use delete:
window.alert = function() { };
delete window.alert;
Here's a working example.

Categories