0

window.location.href is not working for me :/ everthin is running on a localhost with "xampp"

//javascript function 1
function onLogin() {
    window.location = "http://localhost:4000/deletetable";
    console.log("running...");
    console.log(window.location.href);
    return false;
}


// !node.js! function 2 in other file(server.js)
//Delete Table
app.get('/deletetable', (req, res) =>{
    let sql = 'DROP TABLE IF EXISTS users';
    db.query(sql, (err, result) => {
        if(err) throw err;
        console.log(result);
    });
});


//html...
<form onsubmit="onLogin();">
    <div class="wrapper">
       <button class="downloaded-btn"></button>
    </div>
</form>
//only a button...

I tried everthing in the url window.location.href = "url" but nothing works. Thanks for help!

  • Does this answer your question? [JavaScript window.location.href not working in localhost](https://stackoverflow.com/questions/61434625/javascript-window-location-href-not-working-in-localhost) – Javier G.Raya Mar 27 '22 at 19:11
  • No its still only refreshing but no changes in the url :/ – 100procent-skill Mar 27 '22 at 19:15
  • Change `onsubmit="onLogin();"` to `onsubmit="return onLogin();"`. – Ivar Mar 27 '22 at 19:19
  • thanks that worked but in vscode it is highlighting as a problem thats a little bit confusing – 100procent-skill Mar 27 '22 at 19:24
  • `on`-event attributes in general are not recommended. You can use [Michał Perłakowski's approach](https://stackoverflow.com/a/34347610/479156). – Ivar Mar 27 '22 at 19:26
  • mh the `.addEventListener();` function is not working it says `show.html:11 Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at show.html:11:41` in console :/ – 100procent-skill Mar 27 '22 at 19:52
  • @100procent-skill Might be related to [this common issue](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element). – Ivar Mar 27 '22 at 21:06
  • Do you want the user to be able to navigate back? Or do you want to set the new URL and keep them there? – Javax.tech Mar 28 '22 at 02:53
  • i want to keep them back that would be my next question because it is in a `node.js` file – 100procent-skill Mar 28 '22 at 17:39

1 Answers1

0

If you would like to allow the user to navigate backwards, use window.location.assign = "http://localhost:4000/deletetable"

https://www.w3schools.com/jsref/met_loc_assign.asp

//javascript function 1
function onLogin() {
  console.log("running...");
  return window.location.assign="http://localhost:4000/deletetable";
}


// !node.js! function 2 in other file(server.js)
//Delete Table
app.get('/deletetable', (req, res) =>{
    let sql = 'DROP TABLE IF EXISTS users';
    db.query(sql, (err, result) => {
        if(err) throw err;
        console.log(result);
    });
});


//html...
<form onsubmit="onLogin();">
    <div class="wrapper">
       <button class="downloaded-btn"></button>
    </div>
</form>
//only a button...

Otherwise, if you do not want to allow the user to navigate backwards, use: window.location.replace = "http://localhost:4000/deletetable"

https://www.w3schools.com/jsref/met_loc_replace.asp

//javascript function 1
function onLogin() {
 console.log("running...");
 return window.location.replace ="http://localhost:4000/deletetable";
}


// !node.js! function 2 in other file(server.js)
//Delete Table
app.get('/deletetable', (req, res) =>{
    let sql = 'DROP TABLE IF EXISTS users';
    db.query(sql, (err, result) => {
        if(err) throw err;
        console.log(result);
    });
});


//html...
<form onsubmit="onLogin();">
    <div class="wrapper">
       <button class="downloaded-btn"></button>
    </div>
</form>
//only a button...
Javax.tech
  • 45
  • 9
  • If i use `window.location.assign = "http://localhost:4000/deletetable"` the `/deletetable` code only runs if i go back. I want that if i press the button, the `/deletetable` code will run and the user automatically goes back to the previous page. So the user cant see the `/deletetable - tab` – 100procent-skill Mar 28 '22 at 18:18