Simulate long script on page load [duplicate] - javascript

This question already has answers here:
Calling functions with setTimeout()
(6 answers)
Closed 3 years ago.
I am trying to simulate a script that takes a long time to run on page load.
I tried this:
window.onload = function() {
setTimeout(alert("Page Rendered"), 200000);
};
But alert message happens instantly.
What am I doing wrong?

Check the function(). docs
window.onload = function() {
setTimeout(function(){alert("Page Rendered")}, 200000);
};

Related

Script failing to execute more than once [duplicate]

This question already has answers here:
Javascript Function setInterval() works only one time [duplicate]
(2 answers)
JS setInterval executes only once
(2 answers)
Why does the setInterval callback execute only once?
(2 answers)
Closed 6 months ago.
I am trying to make a chatbot for the web game https://skribbl.io but when my javascript script is run, it only runs once.
function send() {
var ic = document.getElementById("inputChat");
ic.value = "ChatBot online";
ic.parentNode.dispatchEvent(new Event('submit', {
bubbles: false,
cancelable: false,
}));
}
setInterval(send(), 1000);
setInterval(send, 1000);
or
setInterval(() => { send() }, 1000);

Is it possible in Javascript to close the window that was just opened by window.open? [duplicate]

This question already has answers here:
How to close a window.open
(2 answers)
Closed 7 years ago.
I'm trying to crash a website a website I'm visiting by blasting it with requests and hopefully overwhelming the server the site is running on. The simple script I made
while ( true ) { window.open(window.location.href); }
keeps requesting the page I'm on until my browser crashes. So, what I'd like to do is the equivalent of
while ( true )
{
window.open(window.location.href);
CloseTheWindowIJustOpened();
}
How do I actually implement that?
Sure thing... the open() object returns you a window object pointing to that new window, and you can manipulate it using it...
example:
var win = window.open("", "myNewWindow", "width=200, height=100");
then, just invoke that variable and use the close() action
win.close();
remember to make your window variable global, so you can invoke it whenever you need:
var myWindow = null;
$(function() {
...
myWindow = window.open(...);
});
function closeMyAmazingWindow() {
myWindow.close();
}
function writeTextOnWindow(txt) {
myWindow.document.write(txt);
}

Open link in new tab after X secs without pop up [duplicate]

This question already has answers here:
Open a URL in a new tab (and not a new window)
(33 answers)
Closed 7 years ago.
I tried several things, but everytime Chrome (and others?) detect a pop up... is possible to bypass it ?
window.open(
'/test.php',
'_blank'
);
}, 2000);
You would have to open the window using javascript, then set a setTimeout for two seconds that would wait for a variable to be set.
The new window would have to set a variable in the parent window to, lets say true.
Then with the setTimeout runs, it checks whether the variable is true, and if not, then opens the link. If it is, then do nothing
var didItOpen = false;
window.open('page.html');
setTimeout(function () { if (!didItOpen) location.href = 'page.html'; }, 2000);
Can you please try the code below?
//1000 = 1 second
setTimeout(function () {
window.open('url here', '_blank);
}, 1000);

How to reroute console.log to document.write [duplicate]

This question already has answers here:
JavaScript Document.Write Replaces All Body Content When Using AJAX
(7 answers)
Closed 9 years ago.
I would like to create a "Console" app for JavaScript so that messages are written to the screen.
console.log("Rerouting log messages to the screen");
console.log = function (message) { document.writeln(message); };
console.log("Log messages are now routed to the screen.");
This works, except that each time something is written to the screen, it wipes out any existing content.
Is there a way to do this?
This is how document.write works. To avoid this you should use document.createElement and document.body.appendChild for example.
For example you can try this code:
console.log = function (message) {
var p = document.createElement( 'p' );
p.innerHTML = message;
document.body.appendChild( p );
};
Read more about document.write at MDN.

Is there a way to deny location.reload call and href modification? [duplicate]

This question already has answers here:
Programmatically disable window.location.reload?
(2 answers)
Closed 9 years ago.
I tried to do this:
location.reload = function() { return false }
But it seems to be denied as it still reloads the page.
EDIT
I do not want to do any harm for a user and lock it into the page. The case where I need it is - integration testing on an existing code base which has location.reload() and location.href = '...' all over the place.
You can use onbeforeunload
window.onbeforeunload = function () {
return confirm("Are you sure you want to leave?");
};
However this is not supported in firefox and opera for that this may be help you
window.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera?

Categories

Resources