I have a post in category A that contain this redirection script:
<script>window.location.replace("http://www.w3schools.com");</script>
However, when I click on A, it also redirects. How to prevent this?
If that redirection script is written in the content section of the post, it is probably being called in with the rest of the content on the category page.
You could add a URL check to your javascript that would look like this:
if(document.URL.indexOf("foo_page.html") >= 0){
window.location.replace("http://www.w3schools.com");
}
And add in your permalink to the if statement
A post has a js redirection script.
Full Stop. If you're redirecting anything in JS, start from there as the first failure point. There's really no reason anything should be redirecting from JS on the front end. There are plenty of redirection plugins out there to handle any redirection rules you might need. Delete the script, redirect the page properly, and move on.
There are of course ways to "fix" what you're trying to do, i.e. strip any scripts out of the post content before outputting it on the category pages, which you should be doing anyway. But you're still better off not having a redirect performed from the front end in JS.