I have the below javascript code. Basically what I want to do is when a user is clicking on the hyperlink, I want to open a new tab, call https://somesite.com/logout, first, wait 5 seconds and then call https://www.somesite.com
This is possible to do? If so, what changes can I make to this code to get that done?
script type="text/javascript">
window.onload = function() {
document.getElementById("mySite").onclick = function() {myFunction()};
function myFunction() {
//your code goes here
var win1= window.open("https://somesite.com/logout");
win1.close();
}
};
</script>
<a href='https://www.somesite.com' target='_blank' id='mysite'>Click Here</a>
window.onload = function() {
document.getElementById("mysite").onclick = function() {myFunction()};
function myFunction() {
//your code goes here
var win1= window.open("https://somesite.com/logout");
win1.close();
window.setTimeout(function() {
window.open("https://somesite.com");
}, 5000);
}
};
getElementById is case sensitive, so be sure to use "mysite". You can set a timeout to open a window after 5000ms (5 seconds).
Also, be aware that browsers will typically block popups by default.
Just add window.setTimeout(window.location.reload(), 5000) after close()
Related
I am trying to set a delay on the mousedown (click) which focuses the newWin.focus in code below:
So in short, the code below works fine. If the popup window is open, it re-focuses on it (bringing it back front) when the user clicks anywhere on the page.
I would like to have a 2 second delay when the user clicks on the page, THEN have it refocus on the new window.
I have spent hours trying to figure this out to no avail using various settimeout's here and there. So, I thought I would show what I have that works and hope someone might explain how my need can be accomplished.
PS: I am still learning my way around javascript and in NO WAY consider myself knowledgeable in the subject.
Thanks in advance!!
<script type="text/javascript">
var newWin;
function openPopup()
{
newWin=window.open('https://www.somesite/url.php','window','width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');
document.onmousedown=focusPopup;
document.onkeyup=focusPopup;
document.onmousemove=focusPopup;
}
function focusPopup(){
if(!newWin.closed){
newWin.focus();
}
}
</script>
You can add a setTimeout to your mousedown handler:
document.onmousedown = () => setTimeout(focusPopup, 2000);
<script type="text/javascript">
var newWin;
function openPopup()
{
newWin=window.open('https://www.somesite/url.php','window','width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');
document.onmousedown=()=>{
setTimeout(focusPopup,2000)
};
document.onkeyup=focusPopup;
document.onmousemove=focusPopup;
}
function focusPopup(){
if(!newWin.closed){
newWin.focus();
}
}
</script>
Just use setTimeout to add the delay.
The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.
var newWin;
function openPopup() {
newWin = window.open('https://www.somesite/url.php', 'window', 'width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');
document.onmousedown = function() {
setTimeout(focusPopup, 2000);
};
document.onkeyup = focusPopup;
document.onmousemove = focusPopup;
}
function focusPopup() {
if (!newWin.closed) {
newWin.focus();
}
}
I'm trying to write a script that simulates a click on a certain button on page. I tried using window.onload but it doesn't work. It seems to me that the problem is that when my script starts executing, the element I want to interact with doesn't exist yet. How can I do that?
Here is what I tried:
window.onload = function() {
document.getElementsByTagName('story')[0].onload = function() {
document.getElementsByTagName('story')[0].click();
};
};
Set click() in a recursive function that uses setTimeout()
window.onload = function() {
function findButton(){
setTimeout(function(){
let btn = document.getElementsByTagName('story')[0]
if(!btn){
findButton()
}else{
btn.click()
}
}, 300)
}
findButton()
}
I have an interactive PDF with a form field button called “Nav_Forward_Page_14” that is set to hidden. I want the button to be visible 10 seconds after the page containing the button loads; so on Page Properties (for the page with the button), I've tried adding the following Javascript on page load, but neither script is working:
setTimeout(function() {
this.getElementById("Nav_Forward_Page_14").style.display = "inline";
}, 10000);
Thinking that I may have the function wrong, I also tried:
setTimeout(function() {
this.getElementById("Nav_Forward_Page_14").display = display.visible;
}, 10000);
Please help me correct the function necessary to show the hidden button after a delay on page load in a PDF. Thank you in advance!
You can use document.body.onload = function(){} which will execute once the DOM body is loaded which is the same as putting your script on the bottom of your <body> tag.
I made the delay 3 seconds to not waste time, you can edit yourself.
Here is a more simplified example:
var delay = 3000;
document.body.onload = function() {
setTimeout(() => {
document.querySelector('button').style.display = 'inline';
}, delay);
}
button {
display: none;
}
<button>Next page</button>
Got this to work in the PDF when added as Javascript to page open:
function runImage() {
this.getField("Nav_Forward_Page_14").display = display.visible
}
run = app.setTimeOut("runImage()", 10000);
I wrote a small jquery code to redirect to google.com after 2 seconds on pressing a button. The code works fine in Firefox, google chrome but not in the Internet Explorer.
I believe the location.assign function is not working since it is the one that is supposed to redirect to another url and all other functions like alert are working but not this one.
The code is as follows:
<script type="text/javascript">
$(document).ready(function(){
$(".prize_select").click(function(){
var timer = setInterval(redirect,2000);
function redirect()
{
location.assign("http://www.google.com");
timer = clearTimeout(timer);
}
});
});
</script>
I would also say that I have already tried these function:
location.assign
window.location
window.location.href
but none of them seems to work in Internet Explorer
You need setTimeout(), not setInterval(). Also, you need location.href, not just window.location. This script says, "When .prize_select is clicked, redirect to "cnn.com" after 2000 ms.
$(document).ready(function() {
$(".prize_select").click(function() {
window.setTimeout(function() {
location.href = 'http://www.cnn.com';
}, 2000);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button class="prize_select">Click me!</button>
Declare the function before using it. e.g.
var timer;
var redirect = function(timer) {
location.assign("http://www.google.com");
timer = clearTimeout(timer);
};
timer = setInterval(function() { redirect(timer); }, 2000);
Update: now passes timer instance
Problem: I have a asp.net button and on click of that I am displaying another window using window.open() at the client side using <script></script>
"I actually, need a popup (alert message) to be displayed on my parent page where my button is located once the user closes the child window."
Couple of things I tried are as follows:
I tried using setTimeOut() to have a time out for some milliseconds. This does not work as the control is not waiting until the time out is complete. It just proceeds to execute next set of code.
I tried using setInterval() but for some reason it is not working for me. Below is the code snippet of that:
$(document).ready(function () {
$('#<%=btnClick.ClientID%>').bind('click', function () {
var newWindow = window.open("http://www.google.com/", "google", 'resizable=1,width=900,height=800,scrollbars=1', '_blank');
newWindow.moveTo(0, 0);
var test = setInterval(function (e) {
if (newWindow.closed) {
alert("HEYY");
clearInterval(test);
__doPostBack("<%= btnClick.UniqueID %>", "");
}
else {
e.preventDefault();
}
}, 5000);
});
});
.
I also tried making an ajax call to open the new window and make it async : false, it again did not help me.
Bring your window and timer variable out of scope of the event handler. You need to do a polling i.e. periodically keep on checking if the windows has been closed. Using setInterval to do a polling will do the job.
var newWin, pollTimer;
$('#btnId').bind('click', function () {
newWin = window.open("...", "...", "");
pollTimer = window.setInterval(function() {
if (newWin.closed) {
window.clearInterval(pollTimer);
callCodeWhenPopupCloses();
}
}, 5000);
});
function callCodeWhenPopupCloses() {
alert("Popup closed.");
...
}