Kunal Vashist
Kunal Vashist

Reputation: 2471

How to detect URL and redirect to 404 if it contains script tag

I have a webpage for example www.testing.example.com/home, now if someone manually add script tag onto the url bar like www.testing.example.com/home/<Script>alert('asd')</script> then it should not give alert, it should redirect to 404 page.

I want to say if my url has script tag then it should redirect to 404 page apart form giving the alert message.

How can this be achieved using Javascript?

Upvotes: 0

Views: 6776

Answers (1)

Gavriel
Gavriel

Reputation: 19237

var regex = new RegExp("%3c.*%3e","i");
var script = regex.exec(window.location.href);
if (script) {
    window.location.href = "/404";
}

Upvotes: 1

Related Questions