Divya Singh
Divya Singh

Reputation: 167

page not redirecting in node

I am trying to redirect to a different page from a function using HTML and Javascript. When I hardcode the URL it gets redirected, but when I put it in a variable it does not. please help.

I am using node version 8.x

  function p(){
     var c ="abc.com";
     var e = `
             <script type="text/javascript">
             function Redirect(){
             window.location=${c};
             }
             document.write("wait");
             setTimeout('Redirect()', 1000);
            </script>`
  return e;

  };

Upvotes: 1

Views: 78

Answers (2)

PA.
PA.

Reputation: 29339

You need to change two things

  1. add quotes around your passed page name

  2. and remove quotes and parentheses from the timeout callback function

         ...
         window.location="${callback}";
         ...
         setTimeout(Redirect, 5000);
         ....
    

Upvotes: 2

Stundji
Stundji

Reputation: 864

You need to fix your setTimeout function:

 setTimeout(Redirect, 5000); // pass in a reference to the Redirect function and don't use quotes.

You can read more about setTimeout here.

Upvotes: 2

Related Questions