skip to Main Content

I’m trying to load a page called "dual.php" using JavaScript, but it didn’t work.

Here is my JavaScript code that I use:

btnSubmitJoin.onclick = function() {
  window.location.href = "dual.php";
}

I want to make it work like the HTML code that i use below:

<button style="--clr:#39FF14" onClick="parent.location='single.php'"><span>Single Player</span><i></i></button>

Anyone know how I can fix this?

I have try using window.location, window.parent.location, window.location.href, and window.location.replace() but it still didn’t work.
I hope someone can tell me where did I do wrong

Here is the screenshot for the details:

The JavaScript code that i use

The button

I want to make it work like this one but in JavaScript style

3

Answers


  1. The correct way is like that:

    <button id="thisbutton" style="..." onclick="window.location.href= '/MyHomePage/dual.php'"> Go to Page </button>
    

    if you want to make this over a Listener: (first delete onclick on HTML code)

    javascript:

    var button = document.getElementById("thisbutton");
    button.addEventListener("click", function() {
      window.location.href = '/MyHomePage/dual.php'";
    });
    

    this is how it is done!

    Login or Signup to reply.
  2. <button style="--clr:#39FF14" onClick="parent.location('single.php')"><span>Single Player</span><i></i></button>
    
    
    const btnSubmitJoin = (path) => {
       window.location.href = path;
    }
    
    Login or Signup to reply.
  3. When you enclose your button in the form and the type is submit then the default behaviour when the button is clicked is to submit the form.

    In your case when you are clicking on the button the enclosed form is getting submitted.

    The solution is to prevent the default behaviour here.

        btnSubmitJoin.onclick = function(e) {
          e.preventDefault();
          window.location.href = "dual.php";
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search