Javascript在点击链接后停留在页面上

Javascript在点击链接后停留在页面上,javascript,Javascript,我有个问题,这是我的代码: <a href="Webshop.php" class="Button" onclick="myFunction()">Webshop</a> <script> function myFunction() { var r = confirm("If you leave this page your purchase will be gone!"); if (r == true) {

我有个问题,这是我的代码:

<a href="Webshop.php" class="Button" onclick="myFunction()">Webshop</a>

<script>
    function myFunction() {
        var r = confirm("If you leave this page your purchase will be gone!");
        if (r == true) {
            window.location = "http://www.titansafes.sgr15.g-o.be/Webshop.php";
        } else {
            return
        }
    }
</script>

函数myFunction(){
var r=confirm(“如果您离开此页面,您的购买将消失!”);
如果(r==true){
window.location=”http://www.titansafes.sgr15.g-o.be/Webshop.php";
}否则{
返回
}
}
如果你点击按钮,我会得到警告,如果他们点击ok,他们会直接返回到webshop,如果他们点击cancel,他们需要留在页面上。但我怎样才能做到最好呢? 我尝试了Window.location(不起作用),return不起作用,这对我来说非常重要。


<a href="javascript:void(0)" class="Button" onclick="myFunction()">Webshop</a>
`
函数myFunction(事件)
{
event.preventDefault();
var r=confirm(“如果您离开此页面,您的购买将消失!”);
如果(r==true)
{
window.location=”http://www.titansafes.sgr15.g-o.be/Webshop.php";
}
}
`

函数myFunction(){
var r=confirm(“如果您离开此页面,您的购买将消失!”);
如果(r==true){
window.location=”http://www.titansafes.sgr15.g-o.be/Webshop.php";
}否则{
}
}

最好不要使用
返回falsereturn false
实际上可以同时执行这两项操作,这不是您真正想要的,重要的是要知道何时使用:

因此,您的处理程序应该如下所示(添加默认值):

另外,作为旁注,尽量避免内联事件,如:
onclick=“code”
,而使用: 元素。addEventListener() 方法和它的IE8和旧的替代品。

然后阅读以下内容:感谢您的回答,该事件不起作用,但确实起了作用:Dyou可以删除href=“Webshop.php”并添加javascript:void(0);或者#
`<a href="Webshop.php" class="Button" onclick="myFunction()">Webshop</a>
<script>
function myFunction(event)
{
 event.preventDefault();
var r=confirm("If you leave this page your purchase will be gone!");
if (r==true)
{
window.location ="http://www.titansafes.sgr15.g-o.be/Webshop.php";
}

}
</script>`
<a href="Webshop.php" class="Button" onclick="myFunction();return false;">Webshop</a>

<script>
    function myFunction() {
        var r = confirm("If you leave this page your purchase will be gone!");
 if (r == true) {
            window.location = "http://www.titansafes.sgr15.g-o.be/Webshop.php";
        } else {
        }
    }
</script>
function myFunction(eventObject) {
    eventObject.preventDefault();
    var r=confirm("If you leave this page your purchase will be gone!");
    if (r==true) {
        window.location ="http://www.titansafes.sgr15.g-o.be/Webshop.php";
    }
}