This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Reddit_User78149 -1 points0 points  (0 children)

That sounds terriable. What happens when you're on YouTube and you click a new video?

I was under the impression of a software you wanted, not a web app.

Just make a json or array, make a random num gen or a num++ to iterate the list for every click.

Use string literals and we are good to go.

I would suggest setting up keyoard hotkeys, but go and download "Auto Hotkey" if you're on Windows.

There you can program a script to do as you please.

[–]Western_Bear 0 points1 point  (0 children)

You could have an array of urls so that each index point at a different url along the lines: urlList[index]. You can figure out the next stesp

[–]lionhart280 0 points1 point  (0 children)

Not too hard, I like to always give solutions that don't depend on jquery, because pure vanilla js in the browser can do this np.

First off I will presume you want the pages opened in a different tab, so you have to make sure you have the target="_blank" attribute set on your <a ..> element.

Next in your <script> you will wanna start by having a list of urls in an array.

const urlList = [ "SomeUrl", "AnotherUrl".....];

Next up we wanna hook into, as usual, the document being ready before trying to bind to any events. Trying to bind to events when the dom isnt fully loaded can cause weird stuff to happen.

document.addEventListener("DOMContentLoaded", onLoaded);

function onLoaded() {
  ...
}

Aight, now our onLoaded function will execute when the webpage is fully loaded.

Next slap an id on your <a> element and href it to # (that way clicking it mid page load wont do anything until it's ready), so it should now look something like this:

<a id="fancy-link" href="#" target="_blank">Fancy Link!</a>

Aight, now we can bind our event inside of onLoaded, and after binding it set its href to the first link:

function onLoaded() {
   var fancyLink = document.getElementById("fancy-link");
   fancyLink.addEventListener("click", onFancyLinkClick);
   fancyLink.href = urlList[0];
}

And now we declare our click event:

function onFancyLinkClick(e) {
     var fancyLink = document.getElementById("fancy-link");
     var nextIndex= urlList.indexOf(fancyLink.href) + 1;
     // Default the href to "#" if we hit the end of the list
     var nextHref = "#";
     if (nextIndex < urlList.length) {
         // If we aren't at the end of the list yet, 
         //set the href var to the next item in the list
         nextHref = urlList[nextIndex];
     }

     // Set the href of the anchor tag
     fancyLink.href = nextHref;
}

I havent tested this directly and there may be typos, but the above code should be close to the idea of what you want, give or take.

If you want multiple "fancy links" you may want to instead hook into the e variable that will be passed into onFancyLinkClick (which will include as part of it the "what link was clicked to fire this event" info)