EnterpriseXL
EnterpriseXL

Reputation: 477

execute Javascript code that is returned res.send()

I am returning (using Node.js) this:

var template = '<a href="http://localhost:7000/">Your Account has been activated. </a>';

return res.send(template);

What I need to do is to return minimal javascript code that will do automatic redirection rather than a tag.

So far user must click the link, I need it to automatically redirect.

Thoughts?

Upvotes: 0

Views: 2013

Answers (2)

Andreas Louv
Andreas Louv

Reputation: 47119

You can set location header:

res.setHeader('Location', 'http://someurl.com');

nodejs http docs

Upvotes: 2

OlivierH
OlivierH

Reputation: 3890

I assume you use ajax and that you return HTML in your response. You can just return javascript :

var template =  '<script type="text/javascript"> window.location.href="http://localhost:7000"; </script>';
return res.send(template);

And then append it to your document. But it's not the cleanest solution.

You could just return the link, and use it in your ajax complete callback.

Upvotes: 2

Related Questions