SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Sep 2001
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with random html code

    ok, so i need a page to display 1 of 2 snippets of code randomly when a the page loads.

    here is what i have tried:

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    
    var randomness = new Array(2)
    randomness[0] = '<a href="javascript:;" onClick="MM_openBrWindow('testimonial_rob.asp','','scrollbars=yes,width=460,height=340')" onMouseOver="MM_swapImage('Image1','','images/polaroid_rob_on.jpg',1)" onMouseOut="MM_swapImgRestore()"><img src="images/polaroid_rob_off.jpg" alt="Testimonials" name="Image1" width="170" height="300" border="0" id="Image1"></a>';
    randomness[1] = '<a href="javascript:;" onClick="MM_openBrWindow('testimonial_nathan.asp','','scrollbars=yes,width=460,height=340')" onMouseOver="MM_swapImage('Image1','','images/polaroid_nathan_on.jpg',1)" onMouseOut="MM_swapImgRestore()"><img src="images/polaroid_nathan_off.jpg" alt="Testimonials" name="Image1" width="170" height="300" border="0" id="Image1"></a>';
    
    var a = Math.floor(Math.random() * randomness.length)
    
    document.write(randomness[a]);
    
    </SCRIPT>
    basically it's an array with 2 html entries. i think it's running into probelms with the JS within the JS, and the quotations are all messed up. im not a coder. im a designer. please help me figure this out. its basically an image that changes when moused over and then loads a popup window when clicked. thanks.
    Steve Davis

  2. #2
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code which you post is not complete. But you can try something like this instead of that MM functions.

    Code:
    <?xml version="1.0" encoding="windows-1250"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <script type="text/javascript">
    var now=new Date();
    var sec=now.getSeconds();
    var ar=['nathan','rob'];
    function createimg(){
    var imc=document.createElement('img');
    imc.src='images/polaroid_'+ar[sec&#37;2]+'_off.jpg';
    imc.setAttribute('alt','Testimonials');
    imc.width='170';
    imc.height='300';
    imc.onmouseover=function(){this.src=(this.src.indexOf('_on')!=-1)?this.src.replace('_on','_off'):this.src.replace('_off','_on');};
    imc.onmouseout=function(){this.src=(this.src.indexOf('_on')!=-1)?this.src.replace('_on','_off'):this.src.replace('_off','_on');};
    imc.onclick=function(){
    var uri='testimonial_'+ar[sec%2]+'.asp';
    var props='scrollbars=yes,width=460,height=340';
    window.open(uri,'',props);
    };
    document.getElementsByTagName('body')[0].appendChild(imc);
    }
    window.onload=function(){createimg();}
    </script>
    </head>
    <body>
    </body>
    </html>

  3. #3
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CraniumDesigns View Post
    ok, so i need a page to display 1 of 2 snippets of code randomly when a the page loads.

    here is what i have tried:

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    
    var randomness = new Array(2)
    randomness[0] = '<a href="javascript:;" onClick="MM_openBrWindow('testimonial_rob.asp','','scrollbars=yes,width=460,height=340')" onMouseOver="MM_swapImage('Image1','','images/polaroid_rob_on.jpg',1)" onMouseOut="MM_swapImgRestore()"><img src="images/polaroid_rob_off.jpg" alt="Testimonials" name="Image1" width="170" height="300" border="0" id="Image1"></a>';
    randomness[1] = '<a href="javascript:;" onClick="MM_openBrWindow('testimonial_nathan.asp','','scrollbars=yes,width=460,height=340')" onMouseOver="MM_swapImage('Image1','','images/polaroid_nathan_on.jpg',1)" onMouseOut="MM_swapImgRestore()"><img src="images/polaroid_nathan_off.jpg" alt="Testimonials" name="Image1" width="170" height="300" border="0" id="Image1"></a>';
    
    var a = Math.floor(Math.random() * randomness.length)
    
    document.write(randomness[a]);
    
    </SCRIPT>
    You are right. The issue is the nesting of quotes. You'll notice that you have single quotes surrounding the whole string - with double AND single quotes within the same string.

    My suggestion would be to surround the array elements with double quotes, nest the single quotes as they are, and escape any additional double quotes within the string:
    Code:
    randomness[0] = "<a href=\"javascript:;\" onClick=\"MM_openBrWindow('testimonial_rob.asp','','scrollbars=yes,width=460,height=340')\" onMouseOver=\"MM_swapImage('Image1','','images/polaroid_rob_on.jpg',1)\" onMouseOut=\"MM_swapImgRestore()\"><img src=\"images/polaroid_rob_off.jpg\" alt=\"Testimonials\" name=\"Image1\" width=\"170\" height=\"300\" border=\"0\" id=\"Image1\"></a>";

  4. #4
    SitePoint Addict
    Join Date
    Sep 2001
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you! ill try that! both of you!
    Steve Davis


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •