-5

Here is the server-side code snipped that handles GET request:

app.get('/', function(req, res){
    res.sendFile(__dirname + "/public/html/index.html");
});

app.get("/new-html", function(req, res){
    console.log("Request /new-html");
    res.sendFile(__dirname + "/public/html/new.html");
})

And this is the code in the client-side that triggers the request:

    $("#load-html").click(function() {
        $.get("/new-html")
    })

The index.hml itself is rendered when the page is loaded and request with root url is made.

What I want is when I hit button with id=load-html to receive and render completely new html file returned by the server.

ChrisG
  • 8,206
  • 4
  • 20
  • 34
Hairi
  • 2,698
  • 1
  • 22
  • 51
  • 3
    Read the docs: http://api.jquery.com/jQuery.get/ You are requesting the file, but discarding it. – ChrisG Dec 20 '17 at 14:52
  • I have read thi. It doesn't tell me how to render a valid html file. I dont want to nest html elements. Instead I want to load a complete html with the DOCTYPE body head and all that sh*t – Hairi Dec 20 '17 at 14:55
  • What exactly do you mean? Put the received document inside an ` – ChrisG Dec 20 '17 at 14:56
  • I am afraid you dont understand my questiion – Hairi Dec 20 '17 at 14:57
  • I'm trying my best, and I'm not slow or anything. You are explaining it badly. WHERE do you want the browser to render the file...? – ChrisG Dec 20 '17 at 14:57
  • @Chris G Never mind Radar155 got the idea. – Hairi Dec 20 '17 at 15:00
  • Duplicate of https://stackoverflow.com/questions/503093/how-to-redirect-to-another-webpage – ChrisG Dec 20 '17 at 15:11

1 Answers1

0

You should use a link tag in your index.html page

<a href="/new-html">some text</a>

If you want to use javascript:

$("#load-html").click(function() {
    window.location.href = 'http://yourdomain.com/new-html'
})
Radar155
  • 1,059
  • 8
  • 25