2

I have a button defined in HTML.

I want to load a page background.html when this button is clicked. But I want to do it from javascript, not from inside the .html file. So on my register.js file I have below:

$(document).ready(function() {
  $("#btnApply").click(function() {
    window.open("background.html");
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="btnApply" value="Register">

But this doesn't load the page. What am I missing?

3
  • What do you mean with load a page? Commented Jan 29, 2018 at 1:14
  • The code works for me as posted in Firefox and Chrome. Clicking the button opens a new tab with the url supplied (a new tab is the usual behavior for a single parameter call to window.open). Make sure background.html is in the same folder as the page with the button. Please update the question with more details (e,g, what you expected, what you got) if you still have trouble. Commented Jan 29, 2018 at 1:51
  • Use Ajax to get content and load on a page where you want. The other way is to route the page with a button click. If you want to load content or a page on a button click without page load use Ajax. Commented Jun 21, 2023 at 1:32

3 Answers 3

1

<input type = "button" id = "btnApply" value = "Register" onclick="loadPage()">

then the function could be in your register.js file as :

    function loadPage()
{

     window.location="background.html";

}
Sign up to request clarification or add additional context in comments.

Comments

0

It might be because popups are blocked on that page. All you need is to allow them (a screenshot from Chrome):

enter image description here

And ensure that you:

  1. Run this code in the browser (otherwise you will not have window object)

  2. background.html is in the same folder as your script

1 Comment

This window.open("background.html"); is totally correct, the OP doesn't need the ./ before the HTML file.
0

The open() method opens a new browser window. If you want it to load from the same window use location object.

window.location.assign("background.html");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.