php 仅在确认后重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18852373/
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
Redirect only after confirmation
提问by Mark
I want to have the user click on a link then have a javascript pop-up with a yes or no choice,but if yes redirect to different url if no, then stay on the same page. I tried the code below but it takes me to the same page when selecting yes or no.
我想让用户点击一个链接,然后有一个带有是或否选择的 javascript 弹出窗口,但如果是,则重定向到不同的 url,如果不是,则留在同一页面上。我尝试了下面的代码,但在选择是或否时它会将我带到同一页面。
function YNconfirm() {
if (window.confirm('Really go to another page?'))
{
alert("You agree")
window.location.href = (http:///mediclaim_portal/logout2.php');
}
else
{
window.location.href = (this.document);
}};
</script>
then
<a href="\mediclaim_portal/logout2.php" onclick="YNconfirm()">Home</a>
回答by Paul Roub
function YNconfirm() {
if (window.confirm('Really go to another page?'))
{
alert("You agree")
window.location.href = 'http:///mediclaim_portal/logout2.php';
}
}
then
然后
<a href="/mediclaim_portal/logout2.php" onclick="YNconfirm(); return false;">Home</a>
回答by Assaf Hershko
If the code running at the onclick event doesn't return false, the href at the happens. i.e., try something like this:
如果在 onclick 事件中运行的代码没有返回 false,则发生 href 处。即,尝试这样的事情:
function YNconfirm() {
if (window.confirm('Really go to another page?'))
{
alert("You agree");
return true;
}
else
return false;
};
</script>
<a href="\mediclaim_portal/logout2.php" onclick="return(YNconfirm());">Home</a>
回答by wiLLiamcastrO
Just do that ;)
就这样做;)
<script type="text/javascript">
function YNconfirm() {
if (window.confirm('Really go to another page?')){
alert("You agree")
//REDIRECT
window.location.href = (http://mediclaim_portal/logout2.php');
}
else{
//DO NOTHING AND STAY IN THE SAME PAGE
//OR SOMETHING ELSE THAT YOU WANT
return false;
}
};
</script>
<!-- IMPORTANT, dont link your 'a', or it will always be submited -->
<a href="javascript:void(0);" onclick="YNconfirm();">Home</a>
回答by SLaks
The browser is navigating to the link after running your click handler.
运行您的点击处理程序后,浏览器将导航到该链接。
You should change your handler to simply return false
to cancel the navigation, and not set location
at all.
您应该将处理程序更改为简单return false
地取消导航,而根本不设置location
。
回答by Vikram Choudhary
## Try this for all url ##
<a href="https://code.jquery.com">Click Here</a>
<script type="text/javascript">
$(document).ready(function(){
$('a').click(function(){
//get a href url
var url = $(this).attr('href');
//check condition for redirection
var answer = confirm("Leave From this page");
if (answer){
document.location.href = url;
}else{
alert("Thanks for sticking around!");
return false;
}
});
});
</script>
回答by CyberClaw
I know this is not codegolf, but this is the solution I usually employ (which is slightly more readable):
我知道这不是 codegolf,但这是我通常使用的解决方案(可读性稍强):
<a onclick="if(confirm("Want to go to the new page?)){location.href = "bla.newpage.bla"}}
While the 'onclick return true' works, it requires internal knowledge on how onclick and href tags interact. I find my way easier on the eyes (specially if you are constantly jumping between C#, JS, SQL, etc)
虽然“onclick return true”有效,但它需要了解 onclick 和 href 标签如何交互的内部知识。我发现我的方式更容易(特别是如果你经常在 C#、JS、SQL 等之间跳转)
回答by Zeeshan Cornelius
<a href="http://mediclaim_portal/logout2.php" onclick="YNconfirm(this, event)">Home</a>
Then write the jquery script as follow
然后编写jquery脚本如下
<script>
function YNconfirm(el,ev){
var confirm = window.confirm("Really go to another page?");
if(confirm){
window.location.href = $(el).attr("href");
}else{
ev.preventDefault();
}
}
</script>
回答by Davinder Kumar
The best way is to use return value of function like this
最好的方法是像这样使用函数的返回值
function YConfirm(
if (window.confirm('Really go to another page?')){
return true;
}
return false;
)
Now simply call this function like this.
现在只需像这样调用这个函数。
<a href="http://mediclaim_portal/logout2.php" onclick="return YConfirm();">Home</a>
Now you don't need to redirect the page with javascript.
现在您不需要使用 javascript 重定向页面。