简体   繁体   English

将页面重定向到新网页

[英]Redirect the page to new webpage

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). 我有one.html和two.html,我的要求是当用户输入one.html的url时,应该显示一个消息“你被重定向到新的网页”的对话框,用户可以点击确定按钮继续或者在10秒内,对话框应自动关闭,用户应重定向到新页面(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. PS:我能够在某种程度上得到结果,但问题是我能够看到one.html及其上的对话框,而用户不应该看到one.html,只有带有消息的对话框和ok按钮应该如果用户没有点击“确定”按钮,则在10秒后显示给用户,它应该重定向到两个页面。

Place this script inside one.html. 将此脚本放在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. 如果你不想看到one.html,你需要另一个页面 - 比如说three.html。 And the code will look like this. 代码看起来像这样。

One.html One.html

<script>
    window.onload = "Three.html";
</script>

And place the above code in Three.html. 并将上述代码放在Three.html中。

If your target is just to redirect viewers of one.html to two.html, you could try this code: 如果您的目标只是将one.html的观看者重定向到two.html,则可以尝试以下代码:

<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 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. 包含所需的javascript文件。
  • window.onload = When a Web page is loaded, the code on the page — whether HTML, CSS, PHP, or some other type. window.onload =加载网页时,页面上的代码 - 无论是HTML,CSS,PHP还是其他类型。
  • confirm() method = The confirm() method displays a dialog box with a specified message, along with an OK and a Cancel button. confirm()方法= confirm()方法显示一个带有指定消息的对话框,以及一个OK和Cancel按钮。
  • If user press on OK button then it will redirect to two.html page with the use of function "window.location.href" 如果用户按下OK按钮,那么它将使用函数“window.location.href”重定向到two.html页面

-- Add This CSS In header - 添加此CSS In标头

<style>
    body
    {
        display:none;
    }
 </style>

That Might Be helpful.... 那可能会有所帮助....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM