1

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?

1

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

2

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.

3
  • There are some reasons actually. For example, one may want to make sure that other scripts will be fired before the redirection occurs. Plugins may not handle it well. See wordpress.stackexchange.com/q/352953/64282
    – Ooker
    Jan 15 '20 at 6:43
  • I'll reiterate, there is no valid reason to perform a redirect on the front end in JS. Pointing to an example of someone using a bad practice doesn't add any validity to the method. I won't get into all of the SEO implications of the method described in the link you provided due to the limitations of this thread. However, the information in that link only further illustrates why redirecting should never be done via JS. If you need to do that to achieve your goal, your first question should be "why am I doing this?". There are much better ways to achieve what the supposed goal was there.
    – KFish
    Jan 16 '20 at 21:24
  • I see. If possible, can you provide an answer there as well?
    – Ooker
    Jan 17 '20 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.