Ok I’m looking to make a “link of the day” button or hyperlink using javascript. I found many examples on how to do this and got it to work using something like this:
[code]
<html>
<head>
<script type=”text/javascript”>
function go_to(url) {
window.location=url;
}
function rand_link() {
var a;
a = 1+Math.round(Math.random()*3);
if (a==1) go_to(“link1”);
if (a==2) go_to(“link2”);
if (a==3) go_to(“link3″);
}
</script>
</head>
<body>
<FORM NAME=”myForm”>
<INPUT TYPE=”button” NAME=”myButton” VALUE=”Random link” onClick=”rand_link()”>
</FORM>
</body>
</html>
Ok this works fine except I would like to have the name of my button/hyperlink display which page it will take you to first as its name rather then “Random link”. Eg “Photos” or something. Now this does not have to be done using a button it could just be a hyperlink that generates a different name and destination each page load.
Is this possible? if so how would I go about it?
Brad
<i>
</i>document.getElementById("myButton").value = 'New Button Text';
<i>
</i><style type="text/css">
#myLinkButton { display:none }
</style>
<script type="text/javascript">
var randomLinks = [
{link: "http://www.linkone.com", name: "Link One" },
{link: "http://www.linktwo.com", name: "Link Two" },
{link: "http://www.linkthree.com", name: "Link Three" },
{link: "http://www.linkfour.com", name: "Link Four" }
]
var randomLinkNumber = Math.floor(Math.random()*randomLinks.length);
function gotoLink() {
window.location = randomLinks[randomLinkNumber].link;
}
onload = function() {
var linkButton = document.getElementById("myLinkButton");
if (linkButton) {
linkButton.value = "Go To "+randomLinks[randomLinkNumber].name;
linkButton.style.display = "inline";
}
}
</script>
[code=html]
<input type="button" id="myLinkButton" onclick="gotoLink()" />
[/code]
Ohhh, you're just trying to get back at me for all the times I posted before you.[/QUOTE]
<i>
</i><html>
<head>
<!--
<style type="text/css">
#myLinkButton { display:none }
</style>
-->
<script type="text/javascript">
var randomLinks = [
{link: "http://192.168.1.21/typo3/index.php?id=576", name: "Sunrise Footage" },
{link: "http://192.168.1.21/typo3/index.php?id=578", name: "GIMP Videos" },
{link: "http://192.168.1.21/typo3/index.php?id=238", name: "IT Articles and Magazines" },
{link: "http://www.linkfour.com", name: "Link Four" }
]
var randomLinkNumber = Math.floor(random()*randomLinks.length);
function gotoLink() {
window.location = randomLinks[randomLinkNumber].link;
}
onload = function() {
var linkButton = document.getElementById("myLinkButton");
if (linkButton) {
linkButton.value = "Go To "+randomLinks[randomLinkNumber].name;
linkButton.style.display = "inline";
}
}
</script>
</head>
<body>
<input type="button" id="myLinkButton" onclick="gotoLink()">
</body>
</html>
<i>
</i>var randomLinkNumber = Math.floor(random()*randomLinks.length);
<i>
</i>var randomLinkNumber = Math.floor(Math.random()*randomLinks.length);
<i>
</i> onload = function() {
var linkButton = document.getElementById("myLinkButton");
if (linkButton) {
linkButton.value = "Go To "+randomLinks[randomLinkNumber].name;
linkButton.style.display = "inline";
}
}
<i>
</i>function initButton {
var linkButton = document.getElementById("myLinkButton");
if (linkButton) {
linkButton.value = "Go To "+randomLinks[randomLinkNumber].name;
linkButton.style.display = "inline";
}
}
if (window.attachEvent)
window.attachEvent("onload",initButton);
else if (window.addEventListener)
window.addEventListener("load",initButton,true);
[code=html]
<html>
<head>
<!--
<style type="text/css">
#myLinkButton { display:none }
</style>
-->
<script type="text/javascript">
var randomLinks = [
{link: "http://192.168.1.21/typo3/index.php?id=576", name: "Sunrise Footage" },
{link: "http://192.168.1.21/typo3/index.php?id=578", name: "GIMP Videos" },
{link: "http://192.168.1.21/typo3/index.php?id=238", name: "IT Articles and Magazines" },
{link: "http://www.linkfour.com", name: "Link Four" }
]
var randomLinkNumber = Math.floor(Math.random()*randomLinks.length);
function gotoLink() {
window.location = randomLinks[randomLinkNumber].link;
}
function initButton {
var linkButton = document.getElementById("myLinkButton");
if (linkButton) {
linkButton.value = "Go To "+randomLinks[randomLinkNumber].name;
linkButton.style.display = "inline";
}
}
if (window.attachEvent)
window.attachEvent("onload",initButton);
else if (window.addEventListener)
window.addEventListener("load",initButton,true);
}
</script>
</head>
<body>
<input type="button" id="myLinkButton" onclick="gotoLink()">
</body>
</html>
[/code]
[code=html]
<html>
<head>
<!--
<style type="text/css">
#myLinkButton { display:none }
</style>
-->
<script type="text/javascript">
var randomLinks = [
{link: "http://192.168.1.21/typo3/index.php?id=576", name: "Sunrise Footage" },
{link: "http://192.168.1.21/typo3/index.php?id=578", name: "GIMP Videos" },
{link: "http://192.168.1.21/typo3/index.php?id=238", name: "IT Articles and Magazines" },
{link: "http://192.168.1.21/typo3/index.php?id=559", name: "Openoffice.org Tutorials" },
{link: "http://192.168.1.21/typo3/index.php?id=557", name: "Inkscape Tutorials" },
{link: "http://192.168.1.21/typo3/index.php?id=558", name: "Scribus Tutorials" },
{link: "http://192.168.1.21/typo3/index.php?id=535", name: "GIMP Tutorials" }
]
var randomLinkNumber = Math.floor(Math.random()*randomLinks.length);
function gotoLink() {
window.location = randomLinks[randomLinkNumber].link;
}
function initButton {
var linkButton = document.getElementById("myLinkButton");
if (linkButton) {
linkButton.value = "Go To "+randomLinks[randomLinkNumber].name;
linkButton.style.display = "inline";
}
}
if (window.attachEvent)
window.attachEvent("onload",initButton);
else if (window.addEventListener)
window.addEventListener("load",initButton,true);
</script>
</head>
<body>
<input type="button" id="myLinkButton" onclick="gotoLink()">
</body>
</html>
[/code]
<i>
</i>function initButton() {
[code=html]
<html>
<head>
<!--
<style type="text/css">
#myLinkButton { display:none }
</style>
-->
<script type="text/javascript">
var randomLinks = [
{link: "http://192.168.1.21/typo3/index.php?id=576", name: "Sunrise Footage" },
{link: "http://192.168.1.21/typo3/index.php?id=578", name: "GIMP Videos" },
{link: "http://192.168.1.21/typo3/index.php?id=238", name: "IT Articles and Magazines" },
{link: "http://192.168.1.21/typo3/index.php?id=559", name: "Openoffice.org Tutorials" },
{link: "http://192.168.1.21/typo3/index.php?id=557", name: "Inkscape Tutorials" },
{link: "http://192.168.1.21/typo3/index.php?id=558", name: "Scribus Tutorials" },
{link: "http://192.168.1.21/typo3/index.php?id=535", name: "GIMP Tutorials" }
]
var randomLinkNumber = Math.floor(Math.random()*randomLinks.length);
function gotoLink() {
window.location = randomLinks[randomLinkNumber].link;
}
function initButton() {
var linkButton = document.getElementById("myLinkButton");
if (linkButton) {
linkButton.value = "Go To "+randomLinks[randomLinkNumber].name;
linkButton.style.display = "inline";
}
}
if (window.attachEvent) {
window.attachEvent("onload",initButton);
}
else if (window.addEventListener) {
window.addEventListener("load",initButton,true);
}
</script>
</head>
<body>
<input type="button" id="myLinkButton" onclick="gotoLink()">
</body>
</html>
[/code]
<i>
</i>function initLink() {
var linkTag = document.getElementById("myLinkTag");
if (linkTag) {
linkTag.innerHTML = "Go To "+randomLinks[randomLinkNumber].name;
linkTag.href = randomLinks[randomLinkNumber].link;
linkTag.style.display = "inline";
}
}
[code=html]
<a id="myLinkTag"></a>
[/code]
<i>
</i>#myLinkTag { display:none; }
0.1.9 — BETA 6.17