0

How can I get the index.html file to open located in /users/arda/ subfolder in order to show that page in the same window using javascript?

I tried window.location.href = "/users/arda/index.html"; (Probably this isn't a correct code.)

(I'm new at codding.)

Arda
  • 13
  • 2
  • 1
    What do you mean with "to open"? Show that page, or get its contents? If you're already at that diretory, you can use relative paths (<- google this) – Martijn Jul 31 '23 at 09:46
  • I think you need to use `window.location.pathname="/users/arda/index.html"` for what you're trying to do. – Tapu Jul 31 '23 at 09:52
  • You mean you want the `index.html` in the `/users/arda/` directory to embed another page, right? – Emre Jul 31 '23 at 10:29
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 01 '23 at 05:10

1 Answers1

0

If what you want is to load the conent of index.html in the window you are at, then it depends on in what JS file you are trying to execute this.

If you are doing this in app.js, then simply:

window.location.href = "index.html";

If you are in login.js:

window.location.href = "./users/arda/index.html"; //note the '.' at the begining

Example of login.js and login.html:

// login.js
function redirect() {
    window.location.href = "./users/arda/index.html"; //note the '.' at the begining;
}
<!-- login.html -->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="style.css" />
  <title>Browser</title>
</head>

<body>
  <h1>
    Login page
  </h1>
  <p>
    This is the login page
  </p>
  <button onclick="redirect()">Redirect to index.html</button>
  <script src="login.js"></script>
</body>

</html>

If what you want is to include the HTML content of index.html inside an other HTML file, then you should check this question.

Clara
  • 51
  • 5