I have one.html and two.html, my requirement is when user enters the url for one.html, a dialog box with message saying "You are redirected to the new Web page" should be displayed and user can click ok button to continue or within 10 seconds the dialog box should close automatically and user should redirect to the new page(two.html). Please suggest.
PS: I can able to get the result to some extent but the issue is i can able to see one.html and the dialog box on it, whereas users should not see one.html, only the dialog box with message and ok button should be dispalyed to the users and after 10 seconds if user is not clicking ok button , it should redirect to two.html page.
Place this script inside one.html. The code is self-explanatory.
<script>
function redirectToPage2(){
var redirectUrl = "two.html"
message= "You will be redirected to Page 2. Click <a href='"+redirectUrl+"'>here</a> if you are not redirected in 10 seconds.";
$(body).innerHTML=message;
setTimeout(function(){
window.location=redirectUrl;
}, 10000); // will redirect after 10 seconds
}
}
window.onload = redirectToPage2;
</script>
If you don't want to see one.html at all, you will need another page - say three.html. And the code will look like this.
One.html
<script>
window.onload = "Three.html";
</script>
And place the above code in Three.html.
If your target is just to redirect viewers of one.html to two.html, you could try this code:
<meta http-equiv="refresh" content="0;two.html">
or if your planning to load something before redirecting, try this:
<script language=javascript>
function redirect(){
// do stuffs
window.location = "two.html";
}
</script>
<body onload="redirect()">
<!-- your content -->
</body>
one.html
<script src="jquery-1.12.2.min.js"></script>
<script>
window.onload = function myFunction() {
if (confirm("Are you sure you want to redirect with two.html page ?") == true) {
window.location.href="two.html";
}
}
</script>
Explanation :
Include javascript file that is required.
window.onload = When a Web page is loaded, the code on the page — whether HTML, CSS, PHP, or some other type.
confirm() method = The confirm() method displays a dialog box with a specified message, along with an OK and a Cancel button.
If user press on OK button then it will redirect to two.html page with the use of function "window.location.href"
-- Add This CSS In header
<style>
body
{
display:none;
}
</style>
That Might Be helpful....
Related
I want the user to be redirected to a different page on click of the browser refresh button but I am unable to do so. I have tried using onbeforeunload but it does not seem to work for my requirement as it gives a message of Leave and Stay but even that does not seem to be working on chrome. So how can I redirect the user to a different page on click of browser refresh button.
The Code that I tried:
<html>
<body>
<script>
window.onbeforeunload = function(e) {
return 'Dialog text here.';
};
</script>
</body>
</html>
Regards,
Rushabh
You have to check performance.getEntriesByType on page load . here is how you can try so .
function checkEvt(){
var evTypep=window.performance.getEntriesByType("navigation")[0].type;
if (evTypep=='reload'){
window.location.replace("http://www.stackoverflow.com");
}
}
checkEvt();
You can try this:
if (performance.navigation.type == 1) {
location.href='https://www.google.com';
} else {
console.log( "Not reloaded");
}
<script type="text/javascript">
<!--
function FP_popUpMsg(msg) {//v1.0
alert(msg);
}
// -->
</script>
</head>
<body style="background-color: #800080" onload="FP_popUpMsg('test message')">
This popup window appears on load of the webpage. I want to format this so that the user cannot access the page unless they click the OK button in the popup. Presently they can click the X in the browser (top right) and still get in
You could always use confirm, which returns true if the 'ok' button was clicked and false if the close or cancel buttons were clicked. MDN has a page on it: https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm. The question at How to stop page load in html static page has some solutions for stopping a page from loading, though they seem hackish and bound to fail at some point. Another solution to that part of the problem would be to redirect to a dedicated page; see How to redirect to another webpage in JavaScript/jQuery? for more.
A sample solution (pure JS, no libraries used here)
<html>
<head>
<script type = "text/javascript">
function redirIfNotConfirmed(msg) {
var confirmed = confirm(msg);
if (!confirmed) window.location.href = "http://my.website.com/dummy_redir_page";
}
redirIfNotConfirmed("click ok to proceed");
</script>
</head>
I want to implement when a user closes its browser, a pop apppear, when user click on leave this page then it will redirect to another page.
For this I have tried
<script language="JavaScript">
window.onbeforeunload = function() {
return 'Are you sure?';
}
</script>
POP up window is appearing. But I don't know how redirect to a page when user click on leave this page.
Plz provide me solutions
By using your code
<script language="JavaScript">
window.onbeforeunload = function() { return 'Are you sure?'; }
</script>
In your html, add redirect <a>
Change url
After click <a>, the confirm message will popup
==============Edit
When clicking <a>, window.onbeforeunload will trigger,
that's mean confirm message popup.
I have a problem. I have a page that when you click a button, a popup with a form is shown. So, I complete some data and I submit. What I want to do is, to submit the form, close the form and refresh the parent page. I don't want to do it with AJAX.
The problem is that in my parent page I have to refresh content with the input information of the form.
So when I refresh, sometimes the data is shown and sometimes not. Do you know why this could happen?
I just use onsubmit="refreshParent()" in my form. The info is stored always in my database, so I think the problem may be that sometimes the refresh catches the new info and sometimes not.
function refreshParent() {
window.opener.location.reload();
window.close();
}
I use this to reload the page that opened a popup window:
<script language="JavaScript">
<!--
function reloadParentPage() {
window.opener.location.href = window.opener.location.href;
if (window.opener.progressWindow) {
window.opener.progressWindow.close()
}
window.close();
}
//-->
</script>
By the way, the code above is called by a link or button in the popup page.
You have a race condition between the script doing the insert and the script reloading the parent.
The solution is to call refreshParent on the page after the submit - that way you know the data is in the database. You don't even have to do it on document ready - return a stub page that just defines and calls refreshParent in the head tag.
In PHP when you run post script, at the end, include this code :
echo '<html><script language="javascript">
parent.location.href="http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'"; // or any other url
</script></html>';
This will output a javascript that will reload the windows.
I have an event called SubmitResponse().
A user is presented with a list of questions and possible responses. After completing the responses, the user clicks the Preview button. If responses are correct, the user clicks the SubmitResponse button and then SubmitResponse() processes it.
Upon clicking the SubmitResponse button, a print screen pops up for the user to print a copy. This is the calling code for the JS print feature.
finsub.Attributes.Add("OnClick", "print_form()")
Works fine but there is one problem. We would like the user to be redirected to the screen that displays results of his/her response with the code below.
Response.Redirect("results.aspx")
What is happening is that once the user clicks the submitResponses button, s/he is immediately redirected to the results.aspx page and the print feature is no longer available.
Is there a way to work around this?
You can do the printing and redirect all via javascript. Here is an example that should work:
function doPrint() {
window.print();
document.location.href = "Somewhere.aspx";
}
Link it to a Button:
<asp:Button ID="btnPrint" runat="server" Text="Print"
OnClientClick="doPrint(); return false;" />
For cross browser compatibility, you need to introduce a delay between the window.print() call and the redirect.
I find the following works well (using JQuery framework to print a page as soon as it loaded):
<script type="text/javascript">
$(document).ready(function () {
window.print();
setTimeout("closePrintView()", 3000);
});
function closePrintView() {
document.location.href = 'somewhere.html';
}
</script>
This can be easily adapted for "Print" buttons and links etc.
you can use this code :
window.print();
window.onafterprint = function(event) {
window.location.href = 'index.php'
};
using window.print() callBack!!
Redirect on client side using javascript/jQuery
onclick="window.print();window.location.href='results.aspx';"
For your case
insub.Attributes.Add("OnClick", "print_form();window.location.href='results.aspx';")
<script type="text/javascript">window.location = 'url.aspx';</script>