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

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

Related

jQuery images preload callback in loop [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 6 years ago.
I am writing script for lightbox gallery. I want to user prelaod techique for elegant image loading. My current script is:
for (var i in imgs) {
var tmp_thumb=$('img');
tmp_thumb.on('load', function(){
O.SG.thumbs.append('<li><a style="background-image:url(\'' + imgs[i].thumb + '\')"></a></li>');
$(this.)remove();
}).attr('src', imgs[i].thumb);
}
The problem is, when load callback is called, loop counter i has almost always the last value from img array.
How to pass to callback the proper value of i from the context of lode
The problem is nothing stops your loop, you are setting a bunch of callbacks and by the time they begin getting called, the value of i has hit the end. Try using deferreds. There are examples online. Here's a pretty decent one: http://aboutcode.net/2013/01/09/load-images-with-jquery-deferred.html

setInterval and setTimeout don't seem to work with a console.log [duplicate]

This question already has answers here:
setTimeout ignores timeout? (Fires immediately) [duplicate]
(3 answers)
Closed 8 years ago.
I am answering my own question in the hopes it will solve someone else's headache:
I could not get the following code to work:
function givePosition(){
var place = $('#two').position();
console.log(place);
}
window.setInterval(givePosition(), 50);
The solution to my problem was to give the function name as a parameter without parentheses, like so:
window.setInterval(functionName, 50);
instead of:
window.setInterval(functionName(), 50)

What use is setInterval with a value of zero? [duplicate]

This question already has answers here:
Why is setTimeout(fn, 0) sometimes useful?
(19 answers)
Closed 9 years ago.
I have seen some JavaScript code that has the following:
var myFunc = function () {
timeout = setTimeout(myFunc, 0);
}
It seems that this would immediately recall the function.
What use case is there for this?
Read this.
In short, it "pauses" the JavaScript execution to let the rendering threads catch up. It gives the browser a chance to finish doing some none-JavaScript things that have been waiting to finish before attending to this new piece of JavaScript.

why does this script only give half the output? [duplicate]

This question already has answers here:
javascript document.write() removes the html from page and display result in a blank page [duplicate]
(3 answers)
Closed 10 years ago.
Here is the function:
function funct1()
{
document.getElementById("demo").innerHTML="Something";
document.write("Mexico!");
}
The output is only:
Mexico
when I click the button on the page, whereas I wanted the output to be:
Something
Mexico
In this instance document.write() doesn't do what you think. It clears the contents of the document, and writes Mexico! (see Quentin's answer for a more detailed explanation of why that is). If you remove this line, you can see that your first statement is executed correctly.
If you want to update the first paragraph, and also add another, you should use the following code:
function funct1()
{
document.getElementById("demo").innerHTML = "Something";
// Create a new <p> element, set its HTML and then append it:
var newP = document.createElement('p');
newP.innerHTML = 'Mexico!';
document.body.appendChild(newP);
}
You can see a working jsFiddle here.
After setting the innerHTML of demo, you call document.write. Since the document is in a closed state at this time, this makes an implicit call to document.open which erases the entire existing document (including the stuff you just assigned with innerHTML). Then you write Mexico! to the new document.
Never call document.write after the DOM is ready (and avoid doing so at any other time). Use DOM manipulation instead.

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