Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8036327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:35:00+00:00 2026-06-05T02:35:00+00:00

I’m trying to figure out how can I make one anchor tag that will

  • 0

I’m trying to figure out how can I make one anchor tag that will everytime change when the page is refresh random from the list I have.

Say I have this list

<a href="http://testpage.com/">This is the first one</a>
<a href="http://testpage.com/">This is the second one</a>
<a href="http://testpage.com/">This is the third one</a>

This is the first one
This is the second one
This is the third one

it is like Link Unit ads that Adsense has but I just want it to do simple random not to do any kind of extra work like check if is relate the topic or not like adsense does.

Please advice me what can I do.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:35:01+00:00Added an answer on June 5, 2026 at 2:35 am
    <a href="http://testpage.com/">
    <script type="text/javascript">
        // This script will replace itself with a random one of these phrases
        var phrases = [
            'This is the first one',
            'This is the second one',
            'This is the third one'
        ];
    
        var scripts = document.getElementsByTagName('script');
        var this_script = scripts[scripts.length - 1];
        this_script.parentNode.replaceChild(document.createTextNode(phrases[Math.floor(Math.random()*phrases.length)]), this_script);
    </script>
    </a>​
    

    JSFiddle


    Breakdown

    Create an array literal containing three strings:

    var phrases = [
        'This is the first one',
        'This is the second one',
        'This is the third one'
    ];
    

    Get a NodeList of all script elements on the page (as the page has loaded up to this point, so all scripts before and including this one):

    var scripts = document.getElementsByTagName('script');
    

    Store the last script in that list (IE. this script element) in this_script:

    var this_script = scripts[scripts.length - 1];
    

    I’ll break this next line up into smaller segments.
    Math.random gives a Number between 0 (inclusive) and 1 (exclusive). Multiplying it by 3 gives an even distribution between 0 (inclusive) and 3 (exclusive) and Math.floor truncates it. This gives a random integer between 0 and 2 inclusive. If you add elements to the array it will update because it uses phrases.length in the calculation, instead of a literal 3:

    Math.floor(Math.random()*phrases.length)
    

    document.createTextNode creates and returns a new Node implementing the Text interface, it’s data is just the value that’s passed in. In this case it’s a random element from the phrases array:

    document.createTextNode(phrases[Math.floor(Math.random()*phrases.length)])
    

    Node.parentNode is self explanatory, in this case the parent of the script element will be the HTMLAnchorElement (that is represented by the <a> tag above the script in the tree). Node.replaceChild takes in two arguments, a new_child and an old_child. We passed in our new Text Node for new child, and this script for old_child, which causes this script to be removed from the DOM and replaced with the Text Node:

    this_script.parentNode.replaceChild(document.createTextNode(phrases[Math.floor(Math.random()*phrases.length)]), this_script);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.