Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅在确认后重定向_Javascript_Php - Fatal编程技术网

Javascript 仅在确认后重定向

Javascript 仅在确认后重定向,javascript,php,Javascript,Php,我想让用户点击一个链接,然后弹出一个javascript窗口,选择是或否,但如果是,则重定向到不同的url。如果不是,则保持在同一页面上。 我尝试了下面的代码,但选择“是”或“否”时会转到同一页 function YNconfirm() { if (window.confirm('Really go to another page?')) { alert("You agree") window.location.href = (http:///mediclaim_portal/lo

我想让用户点击一个链接,然后弹出一个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>
函数YNconfirm(){
if(window.confirm('真的转到另一页?'))
{
警惕(“你同意”)
window.location.href=(http:///mediclaim_portal/logout2.php');
}
其他的
{
window.location.href=(this.document);
}};
然后

运行单击处理程序后,浏览器将导航到该链接

您应该将处理程序更改为simply
return false
,以取消导航,并且根本不设置
location

function YNconfirm() { 
 if (window.confirm('Really go to another page?'))
 {
   alert("You agree") 
   window.location.href = 'http:///mediclaim_portal/logout2.php';
 }
}
然后


如果在onclick事件中运行的代码没有返回false,则会发生错误。 i、 例如,尝试以下方法:

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>
函数YNconfirm(){
if(window.confirm('真的转到另一页?'))
{
警惕(“你同意”);
返回true;
}
其他的
返回false;
};
就这么做吧;)


函数YNconfirm(){
if(window.confirm('真的转到另一页?')){
警惕(“你同意”)
//重定向
window.location.href=(http://mediclaim_portal/logout2.php');
}
否则{
//什么也不做,保持在同一页上
//或者其他你想要的东西
返回false;
}
};

然后编写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>

功能确认(el,ev){
var confirm=window.confirm(“真的转到另一页了吗?”);
如果(确认){
window.location.href=$(el.attr(“href”);
}否则{
ev.preventDefault();
}
}

最好的方法是像这样使用函数的返回值

function YConfirm(
if (window.confirm('Really go to another page?')){
     return true;
 }
 return false;
)
现在只需像这样调用这个函数

<a href="http://mediclaim_portal/logout2.php" onclick="return YConfirm();">Home</a>

现在,您不需要使用javascript重定向页面。

###对所有url尝试此操作##
## 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>
$(文档).ready(函数(){ $('a')。单击(函数(){ //获取一个href url var url=$(this.attr('href'); //检查重定向的条件 var应答=确认(“离开本页”); 若有(答复){ document.location.href=url; }否则{ 警惕(“谢谢你留下来!”); 返回false; } }); });
我知道这不是codegolf,但这是我通常采用的解决方案(可读性稍高):


您似乎忘记了if块第一部分中url之前的
。我认为您只需要
http://
而不需要
http://
。此外,如下面的答案所述返回false也会有所帮助。
function YConfirm(
if (window.confirm('Really go to another page?')){
     return true;
 }
 return false;
)
<a href="http://mediclaim_portal/logout2.php" onclick="return YConfirm();">Home</a>
## 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>
<a onclick="if(confirm("Want to go to the new page?)){location.href = "bla.newpage.bla"}}