Javascript Value Pass

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mchen1117
    New Member
    • Aug 2008
    • 1

    Javascript Value Pass

    I try to display a random image alone with text and link, I find this javascript and did some edit as needed, I don't know how I can pass document.write( ) value to HREF's URL, Please help

    [HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html dir="ltr" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <title>Ultimate r's Example</title>

    <script type="text/javascript">
    Array.prototype .addImgAndText= function(i,t,l) {var n={};n.img=make ImageHTML(i);n. text=t;n.url=l; this[this.length]=n}
    Array.prototype .getRandom=func tion(){return this[Math.floor(Math .random()*this. length)];}

    function makeImageHTML(i ){
    return '<img src="'+i+'" alt="">';
    }
    </script>
    <script type="text/javascript">
    var info=new Array();
    info.addImgAndT ext("dell.gif", "dell","htt p://www.dell.com")
    info.addImgAndT ext("google.gif ","google","htt p://www.google.com" )
    info.addImgAndT ext("gmail.gif" ,"gmail","ht tp://www.gmail.com")
    info.addImgAndT ext("oncall.gif ","yahoo","http ://www.yahoo.com")
    var pickedRecord=in fo.getRandom()
    </script>
    </head>
    <body>

    <table>
    <tbody>
    <tr>
    <td>
    <script type="text/javascript">
    document.write( pickedRecord.im g);
    </script>
    </td>
    <td valign="top">

    <a href='javascrip t:document.writ e(pickedRecord. url);'>
    <script type="text/javascript">
    document.write( pickedRecord.te xt);
    </script></a>
    </td>

    </tr>
    </tbody>
    </table>

    </body>
    </html>[/HTML]
    Last edited by gits; Aug 14 '08, 07:19 AM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Remove the document.write. It's not needed there.

    There is another way to set the href. Give the link an ID, then:
    [code=javascript]document.getEle mentById("id"). href = pickedRecord.ur l;[/code]should be what you need.

    Comment

    Working...