Tim Cross
Tim Cross

Reputation: 79

Add a Link to random text

I have some code which is below which loads up random text messages. I need to add a link to each message so when you click on each message it goes to a different html page. Not sure how to do this! hope you can help

not sure where to put the code to get it to work:

for each message

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--

var r_text = new Array ();
r_text[0] = "I was just thinking about you!";
r_text[1] = "You are a great example for others.";
r_text[2] = "You have great ideas.";
r_text[3] = "When I grow up I want to be you!";
r_text[4] = "I appreciate all of your opinions.";

var i = Math.floor(r_text.length * Math.random());

document.write("<br /><br /><br /><br /><br /><br /><br /><center><FONT SIZE=72><FONT COLOR='#FFFFFF'>" +
r_text[i]  + "</FONT></center><br />");

var bgcolorlist=new Array("#228B22", "#FFD700", "#ADFF2F", "#FF69B4", "#CD5C5C", "#4B0082", "#7CFC00", "#ADD8E6", "#E84643", "#ED0A07", "#EA2907", "#E5294B", "#E00D26", "#FF3030", "#FC7500", "#F95700", "#F43900", "#F95620")

document.body.style.background=bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];

</script><br> <br>
    <br>
    <br>
    <style type="text/css">
<!--
body,td,th {
     color: #000;
     font-family: Helvetica, sans-serif;
</html>

Upvotes: 1

Views: 470

Answers (3)

Jagdish yadav
Jagdish yadav

Reputation: 1

var r_text = new Array();
r_text[0] = {
  msg: "I was just thinking about you!",
  link: "http://www.google.com"
};
r_text[1] = {
  msg: "You are a great example for others.",
  link: "http://www.mylink.com"
};
r_text[2] = {
  msg: "You have great ideas.",
  link: "http://www.yourlink.com"
};

r_text.forEach(function(item){
  document.write("<a href='" + item.link + "'>"+item.msg + "</a>");
  document.write("</br></br>");
});

Upvotes: 0

Vladu Ionut
Vladu Ionut

Reputation: 8193

Add an anchor tag in the HTML with associated link of the message.

var r_text = new Array();
r_text[0] = {
  msg: "I was just thinking about you!",
  link: "http://www.google.com"
};
r_text[1] = {
  msg: "You are a great example for others.",
  link: "http://www.mylink.com"
};
r_text[2] = {
  msg: "You have great ideas.",
  link: "http://www.yourlink.com"
};
r_text[3] = {
  msg: "When I grow up I want to be you!",
  link: "http://www.test.com"
};
r_text[4] = {
  msg: "I appreciate all of your opinions.",
  link: "http://www.facebook.com"
};

var i = Math.floor(r_text.length * Math.random());

document.write("<br /><br /><br /><br /><br /><br /><br /><center><FONT SIZE=72><FONT COLOR='#FFFFFF'><a href='" + r_text[i].link + "'>"+
  r_text[i].msg + "</a></FONT></center><br />");

var bgcolorlist = new Array("#228B22", "#FFD700", "#ADFF2F", "#FF69B4", "#CD5C5C", "#4B0082", "#7CFC00", "#ADD8E6", "#E84643", "#ED0A07", "#EA2907", "#E5294B", "#E00D26", "#FF3030", "#FC7500", "#F95700", "#F43900", "#F95620")

document.body.style.background = bgcolorlist[Math.floor(Math.random() * bgcolorlist.length)];
body,
td,
th {
  color: #000;
  font-family: Helvetica, sans-serif;
}

An ES6 version ( a little bit refactored )

var r_text = new Array();
r_text = [{
    msg: "I was just thinking about you!",
    link: "http://www.google.com"
  },
  {
    msg: "You are a great example for others.",
    link: "http://www.mylink.com"
  },
  {
    msg: "You have great ideas.",
    link: "http://www.yourlink.com"
  }, {
    msg: "When I grow up I want to be you!",
    link: "http://www.test.com"
  }, {
    msg: "I appreciate all of your opinions.",
    link: "http://www.facebook.com"
  }
];

var i = Math.floor(r_text.length * Math.random());

document.write(`<br /><br /><br /><br /><br /><br /><br /><center><FONT SIZE=72><FONT COLOR='#FFFFFF'><a href='${r_text[i].link}'>${r_text[i].msg}</a></FONT></center><br />`);

var bgcolorlist = new Array("#228B22", "#FFD700", "#ADFF2F", "#FF69B4", "#CD5C5C", "#4B0082", "#7CFC00", "#ADD8E6", "#E84643", "#ED0A07", "#EA2907", "#E5294B", "#E00D26", "#FF3030", "#FC7500", "#F95700", "#F43900", "#F95620")

document.body.style.background = bgcolorlist[Math.floor(Math.random() * bgcolorlist.length)];

Upvotes: 1

rjeeb
rjeeb

Reputation: 491

Add a before the text:

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--

var r_text = new Array ();
r_text[0] = "I was just thinking about you!";
r_text[1] = "You are a great example for others.";
r_text[2] = "You have great ideas.";
r_text[3] = "When I grow up I want to be you!";
r_text[4] = "I appreciate all of your opinions.";

var i = Math.floor(r_text.length * Math.random());

document.write("<br /><br /><br /><br /><br /><br /><br /><center><FONT SIZE=72><FONT COLOR='#FFFFFF'><a href='url'>" +
r_text[i]  + "</a></FONT></center><br />");

var bgcolorlist=new Array("#228B22", "#FFD700", "#ADFF2F", "#FF69B4", "#CD5C5C", "#4B0082", "#7CFC00", "#ADD8E6", "#E84643", "#ED0A07", "#EA2907", "#E5294B", "#E00D26", "#FF3030", "#FC7500", "#F95700", "#F43900", "#F95620")

document.body.style.background=bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];

Upvotes: 0

Related Questions