0

(Edited)If i copy the code from the answer given bellow from a different user it works perfectly in a new black html file.Something must be going on with my code not allowing the redirection to happend.I will include some of the html/css of the program

<header class='header'>

<title>Εγγραφή</title>

<link rel="stylesheet" type="text/css" href="mystyle.css" >
<center><img src="logo.png" alt="LOGO" height=100></center>
</header>

<div class="text">
            <p style="font-family: system-ui; font-size: 15pt;">           
            <label for="onoma" style="font-size:15pt;">Διεύθυνση*:</label>&nbsp;
            <input id="address" type="text" name="address" value="" >&nbsp;&nbsp;

<p style="font-family: system-ui; font-size: 15pt;">    
            <div class="text">
            <label for="mera" style="font-size:15pt;">Συνθηματικό πρόσβασης*:</label>&nbsp;
            <input id="password" type="password" name="mera" value="" >&nbsp;&nbsp;
            <label for="minas" style="font-size:15pt;">Κωδικός πρόσβασης*:</label>&nbsp;
            <input id="password2" type="password" name="minas" value="" >&nbsp;&nbsp;

div align="center">   
    <input type="reset" value="Reset">
    <input type="submit" id="btn" value="submit">
    </div>   
    <br><br>&nbsp;<br><br>&nbsp; 
    <script type="text/javascript" src="java.js"></script>

CSS

body {
    margin: 0;
    font-family: system-ui   
   
}
.logocolor {
    background-color:cornflowerblue;
}

.background{
    background-color:wheat;
}
header {
    background-color: rgb(0, 132, 255);
}
.text{
    text-align:left;
    text-indent: 150px;
}
label {
 font-family: system-ui 
}
.change{
    display: flex;
}

Also the current java

function CheckPassword(address, password, password2) {
    if (password === "" || password2 === "" || address === "") {
      alert("Καποιο/Καποια κενά δεν συμπληρώθηκαν σωστά ή ειναι κενά")
    } else {
      alert('message');
      window.location = 'newpage.html';
    }
  }
  
  function ClickMe() {
    CheckPassword(
      document.getElementById('address').value,
      document.getElementById('password').value,
      document.getElementById('password2').value
    );
  }
  
  document.getElementById('btn').addEventListener("click", function() {ClickMe()});
  • You mixed up your arguments; they’re in reverse order compared to the parameters. (Doesn’t affect the issue; just a heads-up.) – Sebastian Simon Dec 03 '22 at 22:33
  • Not reproducible in Firefox, not reproducible in Chrome. Please [edit] the question and specify which browser you’re using (this _includes_ the version number). Use the [browser console (dev tools)](//webmasters.stackexchange.com/q/8525) (hit `F12`), read any errors. Is your ` – Sebastian Simon Dec 03 '22 at 22:37
  • @SebastianSimon i think it's because making use of whitespace in HTML file name not any of your concerns mentioned above. – Erhan Yaşar Dec 03 '22 at 22:53
  • Nothing of the above works i am used Firefox and opera for this and nothing works.It doesnt redirect,i checked the file newpage.html to be in the same folder and nothing works.(yes script is before the

    bracket

    – Andrew Sempros Dec 03 '22 at 23:10
  • @AndrewSempros So no errors in the console? Do you see the alert or not? – Sebastian Simon Dec 03 '22 at 23:14
  • No alerts no nothing – Andrew Sempros Dec 03 '22 at 23:17
  • @ErhanYaşar Evidently, the alert does not even show up, so the file name doesn’t begin to be an issue. Although, it’s a good idea to check this, too, when the time comes: The dev tools provide a **Network** tab. Is the resource _found_ (e.g. HTTP 200 response)? If not, which _actual URL_ is requested? Amend the URL accordingly. Actually, you should do this now with your JS file, then later with `new page.html`. – Sebastian Simon Dec 03 '22 at 23:31
  • Nothing shows up when i press the button for the redirection in the network tab :/ – Andrew Sempros Dec 03 '22 at 23:38
  • @AndrewSempros But your JavaScript file surely shows up, doesn’t it? You’ve shown part of your HTML to be ` `, right? If your JS does not show up in the network tab, then your script isn’t even loaded. Make sure you haven’t enabled any filters that would hide either network logs or error logs or any other logs. – Sebastian Simon Dec 03 '22 at 23:40
  • In the network tab it just says (• Perform a request or the page to see detailed information about network activity. • Click on the button to start performance analysis. ) and only does that mean that the script isnt loaded? – Andrew Sempros Dec 03 '22 at 23:44
  • Have you reloaded the page (without cache — try `Ctrl`+`F5`) and saved all files in your editor to make absolutely sure that the correct HTML is being served? The dev tools provide an **Inspector** / **Elements** tab. Inspect your elements. Is the ` – Sebastian Simon Dec 03 '22 at 23:52
  • Yea it shows the java.js file that javascript is in.Also if i change the type to module it the alerts dont show up it says.Module source URI is not allowed in this document:And it also brings up this error: ross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file: – Andrew Sempros Dec 03 '22 at 23:57
  • Okay, if the JS shows up in the network log, isn’t blocked in any way, is found, i.e. has a 200 response, the Response tab (within the Network) shows the code you expect, the JS also shows up in the (separate) Sources / Debugger tab, the console doesn’t complain about the MIME type of the script or produce any CORS errors, _and_ no relevant message is filtered, then no, your script _is_ loaded and interpreted correctly. And again, I can’t reproduce the issue. [Edit] your post and provide a [mre]. Make sure to provide _exact_ steps anyone can follow to reproduce this issue. – Sebastian Simon Dec 04 '22 at 00:06
  • The errors produced by `type="module"` simply indicate that you’re on the `file:` protocol. This attribute isn’t _currently_ a fix to your issue. – Sebastian Simon Dec 04 '22 at 00:07
  • The exact code you’ve provided shows an error message in the console indicating that `document.getElementById('address')` is `null`. There is no element with `id="address"`; same for `id="password"` and `id="password2"`. The CSS is also not linked. Since you’ve said there are no errors shown in the console (_“So no errors in the console?”_ — _“no nothing”_), this must mean that this isn’t a [mre] yet. Is the `ClickMe` function important for reproducing the error? – Sebastian Simon Dec 04 '22 at 00:24
  • This code must include the id=address id=password id=password2 and the css – Andrew Sempros Dec 04 '22 at 00:30
  • You can’t have two `id` attributes on the same element… and `
    ` is quite misplaced. Did you mean ``? [Validate your HTML](//validator.nu).
    – Sebastian Simon Dec 04 '22 at 00:33
  • I am not quite sure about the head because using it wont show a background behind a logo but when using header it shows.Also what do you exactly mean that i cant have id attributes on the same element?(never mind yea is saw it and changed it to one id per element still didnt change anything) – Andrew Sempros Dec 04 '22 at 00:35
  • So nothing? well thanks for trying anyway – Andrew Sempros Dec 04 '22 at 01:20
  • @SebastianSimon HTML that's updated later on was not in a correct form mostly and after a couple fixes with naming as well, it's now working with the updated answer below. – Erhan Yaşar Dec 04 '22 at 09:39

2 Answers2

0

Edit: The answer is updated as per the question's update. Hence it still contains the same reproduced working code sample enquirer provided.

It's working as per the snippet and the new codesandbox here. The older one is also still here for you to compare them even there's not much change but rather typos needed to be fixed.

I made a couple necessary fixes and changes in order to provide best practices even some parts still same in order to not make it more complicated for you. In order to get an HTML page successfully working, you just need to be sure that;

  • HTML is valid (you may not understand it when you look at it on a regular page but all tags should be closed respectively),
  • All necessary files (Js/CSS) imported within that HTML file accordingly,
  • The HTML page desired to be redirected exists and correctly named besides matching the exact path within the expression.

You can also successfully redirect via methods below;

  • window.location.href = 'newPage.html'
  • window.location.replace('newPage.html');

You can take a look at here for their difference.

function CheckPassword(address, password, password2) {
  if (password === "" || password2 === "" || address === "") {
    alert("Καποιο/Καποια κενά δεν συμπληρώθηκαν σωστά ή ειναι κενά")
  } else {
    alert('message');
    window.location = 'https://stackoverflow.com';
  }
}

function ClickMe() {
  CheckPassword(
    document.getElementById('address').value,
    document.getElementById('password').value,
    document.getElementById('password2').value
  );
}

document.getElementById('btn').addEventListener("click", function() {ClickMe()});
<input type="text" id="address" />
<input type="text" id="password" />
<input type="text" id="password2" />

<button id="btn">Button</button>
Erhan Yaşar
  • 847
  • 1
  • 9
  • 25
  • This is the button with the java code insert in html and it doesnt work.Is there a mistake here? – Andrew Sempros Dec 03 '22 at 23:13
  • @AndrewSempros I just updated the codesandbox as well and there's not only one wrong issue. I corrected them all like fixing HTML file, including related js file within it and now it works again. You can compare it with previous one and the above snippet in order to get it more meaningful with a result. You're welcome to ask more. – Erhan Yaşar Dec 04 '22 at 09:37
  • For your information, I renamed your js file as `index.js` since it's not Java as you might not know the difference but a good to prefer more meaningfull naming all over your environments. – Erhan Yaşar Dec 04 '22 at 09:42
  • By including the related js file you mean by just implementing the js in the html?And what fixes did you do on the html file? – Andrew Sempros Dec 04 '22 at 10:51
  • You didn't close HTML tags specifically
    at the end. And also you should rename `ClickMe` function as `clickMe` for both of the occurences. You can check it on the sandbox I provided above and correct them with yours. It's exactly what you provided but in a corrected form.
    – Erhan Yaşar Dec 04 '22 at 10:59
  • still doesnt work :/ i am starting to think this is impossible to run correctly – Andrew Sempros Dec 04 '22 at 11:06
  • I mean how can this possible run on its on but when i paste it in my code it doesnt redirect.Like how does that even work – Andrew Sempros Dec 04 '22 at 11:07
  • 1
    It's working for all of the people but you probably don't spend enough time on checking all the file names exactly matching the paths specified within HTML. For debugging purpose, you can omit everything, and start to add each pieces with my code one by one. There's no magic stick, you should spend hours, days, weeks to not to make a mistake or how to realize and correct them after checking maybe million times like everyone else. Otherwise no one can help. – Erhan Yaşar Dec 04 '22 at 11:11
  • 1
    @AndrewSempros Probably wouldn’t have phrased it like Erhan, but they’re right: your code is working for us. Unfortunately, you didn’t provide a [mre] from the start, so all we could do is fill in the blanks with the _correct assumptions_, e.g. a correct ` – Sebastian Simon Dec 04 '22 at 15:05
  • 1
    Just to illustrate: [this validator output](//i.stack.imgur.com/1j1Ot.png) is what we see for the HTML _currently_ in your question. The validator tells you exactly what’s wrong and where the error is. If you don’t understand a certain thing, just look it up in the documentation: e.g. [``](//developer.mozilla.org/en/docs/Web/HTML/Element/head), [` – Sebastian Simon Dec 04 '22 at 15:22
-1

The current web URL is located in window.location.href, not window.location. Here's an example navigation snippet:

document.getElementById("navigate").addEventListener("click", (e) => {
  window.location.href = "https://example.com/";
})
<button type="button" id="navigate">Navigate</button>

function CheckPassword(address, password, password2) {
  if ((password == "") || (password2 == "") || (address == "")) {
    alert("Καποιο/Καποια κενά δεν συμπληρώθηκαν σωστά ή ειναι κενά")
  } else {
    alert('message');
    window.location.href = 'new page.html';
  }
}

function ClickMe() {
  CheckPassword(document.getElementById('password2').value, document.getElementById('password').value, document.getElementById('address').value)
}

document.getElementById('btn').addEventListener("click", function() {
  ClickMe()
});

So your corrected code would use window.location.href instead of window.loaction.

human bean
  • 847
  • 3
  • 15