简体   繁体   中英

How can I make a button redirect my page to another page using addEventListener?

Currently after searching the internet I've come up with this code:

function button(){


        var button= getElementById("Sailthru");

    button.addEventListener(MouseEvent.CLICK, onClick, false, 0, true)
  function onClick(e:MouseEvent):void{
navigateToURL(new URLRequest("http://www.sailthru.com/"), "_blank");
};

Though it doesn't work, and I am unsure how to proceed. If there are no ways to make it work using this, then I will happily use another method. However I would like to use this method since I've been working on this for a while.

What language mix is this?

The easiest way to go about this using plain vanilla javascript is something like this:

var button = document.getElementById("Sailthru");

button.addEventListener("click", function(){
    document.location.href = 'http://www.sailthru.com';
});

Alternatively, this uses HTTP redirects.

HTML

<button id="btn">
  Click
</button>

JS

btn.onclick = function() {
  window.location.replace("http://www.sailthru.com/");
}

you can try

window.open or window.location based on your demand

window.open(' http://test.com ','_blank');

window.location="http://test.com";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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