Reader (and note to future self),
I have been looking for a way to use the “?” (question mark) and trail with a string while writing a web link. My purpose is in trying to send the clicked link elsewhere. However, I am not wanting to code a permanent redirect as in redirecting to a moved address or site location, nor am I using it to pass search terms and their results from a search database (i.e., https://stackoverflow.com/search?q=redirect+javascript). I am not at this time of writing needing it to trackback affiliate links or such, just wanting to use the method mainly because of its looking so proper; and that so many other websites use redirects and navigation methods that use the question mark (“?”) seamlessly within visiting their site and its pages.
I found a StackOverflow (the link opens in a new tab) article question and its answers that seem to be what I’ve been searching for in this instance.
In the page of results, I found when looking for “redirect javascript” the page “How do I redirect to another webpage?” and the section which seems to be what I am looking for is the answer section that starts and says, “So, the question is how to make a redirect page, and not how to redirect to a website?
You only need to use JavaScript for this. Here is some tiny code that will create a dynamic redirect page.”
(“How do I redirect to another webpage?” https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage – the link opens in a new tab)
The modified code I am using then is:
<script>
// <!-- https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage | "How do I redirect to another webpage?" -->
// So, the question is how to make a redirect page, and not how to redirect to a website?
// You only need to use JavaScript for this. Here is some tiny code that will create a dynamic redirect page.
// < script>
// var url = window.location.search.split('url=')[1]; // Get the URL after ?url=
// if( url ) window.location.replace(url);
// </ script>
// So say you just put this snippet into a redirect/index.html file on your website you can use it like so.
// http://www.mywebsite.com/redirect?url=http://stackoverflow.com
// And if you go to that link it will automatically redirect you to stackoverflow.com.
var url = window.location.search.split('url=')[1]; // Get the URL after ?url=
if( url ) window.location.replace(url);
</script>
It seems as if I have run across this article in the past, because, when I looked to bookmark the link in Chrome while reading it, the bookmark symbol already showed it in my bookmarks. So I changed its location in my bookmarks to find it more quickly the next time I need to refer to this aid.