4

My code goes something like this -

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>


    <script type='text/javascript'>

    var random_generator;
    document.write('Random Selection: ');
    var arr=['One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten'];
    var randomnumber=Math.floor(Math.random()*10);
    random_generator=arr[randomnumber];
    document.write(random_generator+ '<br>');


    </script>

<style type="text/css">

</style>
<a href='www.xyz.com/Index1/Index2/Variable/Index4'>Good Morning</a>
</head>
<body> 
</body>  
</html>

In place of Variable in the tag, I want to use random_generator Javascript variable. How do I do that? Thanks in Advance.

NOTE: random_generator doesn't replace the entire link. Only a part of the link (/Variable/). Depending on this value, the user is shown different pages.

3
  • In place of the href or the text? Sorry I'm confused. Commented Oct 22, 2012 at 20:07
  • In place of Variable in the href link. Different address link depending on the value of Variable ( /Variable/ ) in the href tag. Commented Oct 22, 2012 at 20:09
  • My apologies. I missed that. Thank you for your patience. Commented Oct 22, 2012 at 20:12

5 Answers 5

7

Or maybe you could just write the link as inline js.

​<script>
document.write('<a href="http://www.xyz.com/Index1/Index2/' + random_generator + '/Index4">hello</a>');
</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure if i understand your problem. But the following should work if you are trying to dynamically set the link to href (assuming you are not using jquery).

<a href='http://www.xyz.com/Index1/Index2/Variable/Index4' id="index">Good Morning</a>
<script>
    var newUrl = document.getElementById('index').href;
    newUrl = newUrl.replace("Variable", random_generator);
    document.getElementById('index').href = newUrl;
</script>

Comments

0

You can use jQuery to update the dom element when the script gets executed

First you must give an id to your href

<a href="#" id="myHref"

and then

$('#myHref').attr('href', variable)

Or you can change the link text var varText = "This is a link"; $('#myHref').text(varText);

2 Comments

Or just document.getElementById
You mean $('#myHref').attr('href', variable) ?
0

pretty sure this will wrk

   document.getElementById('myHref').href = variable;

Comments

0

As far as I know this would not be possible. You could do one of two things:

1 Change the HREF attribute when the document loads. With JQuery you could do something like

$(function() {
 $('a').attr('href', 'www.xyz.com/Index1/Index2/' + random_generator + '/Index4');
});

2 Catch the click event and redirect it with javascript, this would mean even if the user clicked the link multiple times without refreshing the page it would be random.

$(function() {
 $('a').on('click', function(e) {
   e.preventDefault();
   window.location = 'www.xyz.com/Index1/Index2/' + random_generator + '/Index4';
 });
});

If you don't wish you use jquery I'm sure there's plenty examples online to do this with plain javascript.

1 Comment

Appreciate your response. Thank You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.