Javascript:通过弹出窗口重定向到另一个网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16320478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:11:28  来源:igfitidea点击:

Javascript : Redirecting to another webpage through a popup

javascriptredirectpopupsandbox

提问by Erki Kuusk

I'm quite bad with Javascript and I can't work out a solution I need help to. I'm having a website and I'm trying to make a redirection to another site, but through a popup.

我对 Javascript 很不擅长,我无法找到需要帮助的解决方案。我有一个网站,我试图重定向到另一个网站,但通过一个弹出窗口。

Example :

例子 :

<script>alert(This will prompt up the message)</scrip>

<script>window.location="http://This-will-redirect-me-to-another-link.com";</scrip>

Like you can see I could simply use the second javascript to redirect the persons to another page, but due some reasons I can't use it as it will work only for half of the page(the script would be kinda 'sandboxed'), but if I'd make a popup(the first alert script) the second script would get out of the 'sandbox'. Is there anyone who has any ideas how I should implement this or can it be done otherwise with PHP or HTML?

如您所见,我可以简单地使用第二个 javascript 将人员重定向到另一个页面,但由于某些原因我不能使用它,因为它只能在页面的一半中工作(脚本有点像“沙盒”),但是如果我制作一个弹出窗口(第一个警报脚本),第二个脚本就会脱离“沙盒”。有没有人对我应该如何实现这个有任何想法,或者可以用 PHP 或 HTML 来完成吗?



Thanks for both of your replies yet these doesn't help me out yet although I'm quite sure that your help will. I'm having a MyBB forum and there's a shoutbox for it which I'm using. There's a command which will change the notice of the shoutbox and the command is as such /notice New notice | But I noted that the new notice can be changed with javascript and it'll work such as /notice js code here | Then I thought that what if I would make such a javascript that would redirect people to another webpage. As I'm having such a forum where it's needed to redirect from the main page to another one, I'd like to apply it. Then Staffs could do it in the forum very well, but there's a problem. by adding

感谢您的两个回复,尽管我很确定您的帮助会帮助我,但这些并没有帮助我。我有一个 MyBB 论坛,并且有一个我正在使用的聊天框。有一个命令可以改变呼喊框的通知,命令就是这样 /notice 新通知 | 但我注意到新的通知可以用 javascript 改变,它会像这里的 /notice js 代码一样工作 | 然后我想,如果我制作这样一个 javascript 将人们重定向到另一个网页会怎样。由于我有这样一个论坛,需要从主页重定向到另一个论坛,因此我想应用它。然后工作人员可以在论坛上很好地做到这一点,但是有一个问题。通过添加

/notice window.location="http://This-will-redirect-me-to-another-link.com"; It'll affect only the shoutbox and shoutbox is being redirected to another webpage, but as an alert works for the whole forum I thought maybe I can redirect them to somewhere else with the alert. I want to know is it possible with just one script then Staffs would be able to do it. I know it's a serious security risk & it can be otherwise also, but I'd really like to experiment with it.

/notice window.location="http://This-will-redirect-me-to-another-link.com"; 它只会影响shoutbox 并且shoutbox 被重定向到另一个网页,但是由于警报适用于整个论坛,我想也许我可以将它们重定向到带有警报的其他地方。我想知道是否有可能只用一个脚本然后工作人员就可以做到。我知道这是一个严重的安全风险,另外也可能是,但我真的很想尝试一下。

I hope someone can help. :)

我希望有人能帮帮忙。:)

回答by Guanxi

Use Confim and capture response to redirect. This popup will show two buttons - OK and Cancel.

使用 Confim 和捕获响应来重定向。此弹出窗口将显示两个按钮 - 确定和取消。

if(confirm("Popup Message")){
   window.location = "your intended destination";
}else {//do nothing. This will fire if cancel is clicked.}

回答by dougis

This will do it, but what is the trigger that the user is going to perform to make the redirect happen? I would also say you will be WAY better off to use something like a jQuery dialog than an alert box In the head of the HTML

这样就可以了,但是用户要执行重定向的触发器是什么?我还想说,在 HTML 的头部使用 jQuery 对话框之类的东西比使用警告框要好得多

<script>
function changePage(whereToGo, messageText)
{
    alert(messageText);
    window.location=whereToGo;
}
</script>

Then in the HTML itself

然后在 HTML 本身中

<script>changePage("http://This-will-redirect-me-to-another-link.com", "The message you want to show");</script>

Note the complete of any error checks

注意任何错误检查的完成

OK, then simply try this

好的,然后简单地试试这个

<script>
alert("Whatever you want to say") && window.location="http://yourwebsitehere.com";
</script>

That will perform both actions in a single statement

这将在单个语句中执行两个操作