简体   繁体   English

单击链接后,JavaScript停留在页面上

[英]Javascript staying on a page after clicking on a link

I have a question , this is my code : 我有一个问题,这是我的代码:

<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>

If you click on the button I get the alert , if they click ok , they direct back to the webshop , if they click cancel , they need to stay on the page. 如果单击“我得到警报”按钮,则单击“确定”,他们将直接返回到网上商店,如果单击“取消”,则需要保留在页面上。 But how can I do that best? 但是我该怎么做呢? I tried Window.location ( doesn't work ), return doesn't work , and it's quite important for me. 我尝试了Window.location(不起作用),return不起作用,这对我来说很重要。

<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>
<a href="javascript:void(0)" class="Button" onclick="myFunction()">Webshop</a>
`<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>`

It's better not to use return false; 最好不要使用return false; but instead in the event handler function call the preventDefault method on the event object argument that is being passed to it. 而是在事件处理函数中,对要传递给它的事件对象参数调用preventDefault方法。 The event object is the single argument that's passed to every event handler function. 事件对象是传递给每个事件处理程序函数的单个参数。 Two important methods on it are the preventDefault, which stop the default event action (in this case the navigation, as it's a link. The second important method is the stopPropagation which will stop the event from being triggered on parent elements. Using return false will actually do both, which isn't nesecerally what you want, it's important to know when to use what: http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/ 它的两个重要方法是preventDefault,它阻止默认事件操作(在本例中为导航,因为它是一个链接)。第二个重要方法是stopPropagation,它将阻止事件在父元素上触发。使用return false将实际上两者都可以,这并不是您想要的,重要的是要知道何时使用: http : //fuelyourcoding.com/jquery-events-stop-misusing-return-false/

So your handler should look something like this (adding the preventDefault): 因此,您的处理程序应如下所示(添加preventDefault):

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";
    }
}

Also, as a side note, try to avoid inline events like: onclick="CODE" instead use the: element.addEventListener() method and it's IE8 and older alternatives. 另外,作为一个旁注,请尝试避免内联事件,例如: onclick="CODE"而不要使用:element.addEventListener()方法,它是IE8和更早版本的替代方法。 http://robertnyman.com/2008/11/20/why-inline-css-and-javascript-code-is-such-a-bad-thing/ http://robertnyman.com/2008/11/20/why-inline-css-and-javascript-code-is-such-a-bad-thing/

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

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