Bringing back an "overridden" window method [duplicate] - javascript

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.

Related

Transform alert to console.log [duplicate]

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')

Javascript code doesn't execute after the page changes [duplicate]

This question already has answers here:
Is there a way to have an onload callback after changing window.location.href?
(3 answers)
Closed 4 years ago.
So, I'm trying to run this code
document.location = "https://stackoverflow.com/questions/ask";
document.onload = function(){document.getElementById("title").value="My question";};
but it turns out that my function doesn't run the function. You can observe this by doing
document.location = "https://stackoverflow.com/questions/ask";
document.onload = function(){document.getElementById("title").value="My question";alert('Hi');};
My question is, what am I doing wrong? Why doesn't the function run?
Something similar was asked here!
Here is a quote of the most important part of the accepted and most upvoted answer:
No, you cannot do it the way you want. Loading a new page closes the current document and starts loading a new document. Any code in your current document will no longer be active when the new page starts to load. - Source: Answer by jfriend00

How to get the self-DOM object in callback [duplicate]

This question already has answers here:
jQuery Button Click 'this'
(3 answers)
Closed 5 years ago.
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
this.addClass('btn-default'); it didn't works.
I would like to get self-dom object(in this case $("#subPanel") itself) from inside the call back.
It might be easy problem, so I try to googled around.
However I couldn't get straight answer.
could you help me ??
Inspect this and you will see it's not a jquery object but a DOM element which does not have an addClass method. Try:
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
$(this).addClass('btn-default')
})
Example: https://jsfiddle.net/14s0h3dr/

Javascript handling inputs "value"-attribute changes [duplicate]

This question already has answers here:
Listening for variable changes in JavaScript
(29 answers)
Listener for property value changes in a Javascript object
(8 answers)
Closed 7 years ago.
There's a third-party js-code, which changes my inputs values like this:
some_third_party_func = function() {
my_input.value = some_val;
};
I want to track all the changes of my_input.value somehow. Modifying a third-party js is the thing I want to avoid. Probably, events can be useful, but which one? The oninput event looks acceptable, except for handling only keyboard input because of some mysterious reasons.
my_input.oninput = function() { alert("my_input.oninput"); };
// ^^ alert() runs only when inputting from the keyboard
Here you can try the issue (how it looks for me).
Is there any method to do this (with jQuery, maybe)?

JavaScript - Get calling object [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript how do you find the caller function?
Is there a way to get the value of this from the function which has called the current function?
Look at this:
function TraceMySelf(){
console.log(this);
}
function A(){
TraceMySelf();
console.log(this);
}
var a = new A();
When this code is executed, the console displays first the window object and then the a object. How can I make the code display the a object twice, with only changing line 2? I know that I could apply the function inside A with this, but that isn't what I want.
Is this possible?
I think this is the answer to your question: StackOverflow 280389
However, I think the right answer is "don't do that". I think it runs counter to how JavaScript is designed.
It might also be worth looking at jQuery Proxy for another way of linking function and object.

Categories