Javascript popups: Submit form, show “wait” screen for 5 seconds, then auto-close popup

落爺英雄遲暮 提交于 2019-12-14 02:24:55

问题


I have a popup that requires the user to fill out a form. Once the form is submitted, I need to take them to a "Waiting" screen inside that same popup while their info is checked. This is a prototype, so it only needs a delay of about 5 seconds as nothing is actually being checked. Then I'd like to close the popup automatically after that delay, instead of using window.close(); attached to a button.

Here's the script I'm using: (edited to remove inline js)

function centeredPopup(url,winName,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
popupWindow = window.open(url,winName,settings)
}

$('#linkPopup').click(function(){
    $(this).click(centeredPopup("popup.php","Popup Name","680","560"));

    if(window.location.href=='popup_waiting.php'){
        setTimeout(function() {
        close.window();
        }, 5000);
    }   
});

How I'm firing the popups:

<a class="button" id="linkPopup">Popup Link</a>

Using my code above, the form just submits to the waiting screen but doesn't close it out after the specified duration, so I'm assuming there's something wrong with my syntax or structure.

Expected Behavior:


回答1:


here's a plunker: http://plnkr.co/edit/EAWf6EPauBuz36Wu8Erk?p=info

first thing's first, consider this answer on why you shouldn't use inline javascript.

in order to open a popup window you could use:
window.open();

window.open(strUrl, strWindowName[, strWindowFeatures]);

after the popup opens, you can close it with
window.close();

you'll need to set a timer, that'll wait 5 seconds and then close the popup

window.setTimeout(func, delay, [param1, param2, ...]);




回答2:


I solved this by including the following into the waiting page popup:

<script type="text/javascript">
$(document).ready(function() {
setTimeout(function(){ 
    window.opener.location.href = "parentpage.php";
    window.close();
}, 2000); 
});
</script>

That does the trick.



来源:https://stackoverflow.com/questions/21392110/javascript-popups-submit-form-show-wait-screen-for-5-seconds-then-auto-clos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!