Solved

Random Number in a Javascript call.

Posted on 2004-10-26
277 Views
Last Modified: 2012-05-05
In an asp page I am making a javascript call, one of the parameters is called 'first'. I want to stick a random number in there from 1-10. What's the best way to do this?
Here is the javascript:
<SCRIPT language="JavaScript" SRC="http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=3"></SCRIPT>

So where first=3, it should be first=(1-10).

Thanks!
0
Question by:seahawk9
    10 Comments
     
    LVL 2

    Expert Comment

    by:MGrassman
     

     // Math.random() returns a decimal number between
     // 0 and 1.  You multiple the result by the range
     // of numbers you want.

     // This will generate 0-10.
         var rannum = 0
         while(rannum == 0) {
             rannum = Math.random()*10;
         }

     // This would generate 145-200
     // rannum = Math.random()*55 + 145;

     // rannum is now a decimal value, to turn it into
     // an integer use Math.round to round it to the nearest
     // integer.  Math.floor(rannum) to always round down and
     // Math.ceil(rannum) to round up.
         
         rannum = Math.round(rannum);


    location.href = "http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=" + rannum


    location.href will redirect you to the url specified
    Hope this helps
    0
     

    Author Comment

    by:seahawk9
    Can you give me the exact syntax? It looks like your solution is in javascript not asp. So how would it look?
    Would is be something like this?:

    <SCRIPT language="JavaScript"

    // This will generate 0-10.
         var rannum = 0
         while(rannum == 0) {
             rannum = Math.random()*10;
         }

     // This would generate 145-200
     // rannum = Math.random()*55 + 145;

     // rannum is now a decimal value, to turn it into
     // an integer use Math.round to round it to the nearest
     // integer.  Math.floor(rannum) to always round down and
     // Math.ceil(rannum) to round up.
         
         rannum = Math.round(rannum);


    location.href = "http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=" + rannum


    SRC=location.href></SCRIPT>
    0
     

    Author Comment

    by:seahawk9
    FYI, that gives me back a blank result.
    0
     
    LVL 2

    Expert Comment

    by:MGrassman
    This is all you need

    Take out the src=

    just try the following below

    ------------------------------------------------------------
    <SCRIPT language="JavaScript">
    var rannum = 0
    rannum = Math.random()*10;
    rannum = Math.ceil(rannum);


    location.href = "http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=" + rannum;
    </SCRIPT>
    0
     

    Author Comment

    by:seahawk9
    Thanks, but that totally moves me off my asp page. I want the SRC embedded in my page, not taking control of the page.
    0
     
    LVL 2

    Expert Comment

    by:MGrassman
    I would just use vbScript to generate the random number.  I'm not quite sure why you are using "src" in the javascript or what "src" is used for.

    If you could elaberate on this a little more that would be great.

    Thanks,
    0
     

    Author Comment

    by:seahawk9
    Here is the code on a broader scale.
    <% If irsSearchCount Mod 5 = 0 Then %>
    <tr>
    <td ALIGN="CENTER" COLSPAN="7" BGCOLOR="#FFFFFF"><font FACE="HELVETICA" SIZE="-1">
    <hr WIDTH="80%">
    <SCRIPT language="JavaScript" SRC="http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=2"></SCRIPT>
    <hr WIDTH="80%">
    </td>
    </tr>
    <%
    End If
    irsSearchCount = irsSearchCount + 1
    %>

    I have tried doing this:
    <SCRIPT language="JavaScript" SRC="http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=<%irsSearchCount%>"></SCRIPT>

    And I get a type mismatch error.
    0
     
    LVL 2

    Expert Comment

    by:MGrassman
    You can't use asp inside of javascript.

    The only reason to use a "src" attribute in a "<script>" tag is to specify an exteral javascript file that you would like to use instead of typeing the javascript in the page itself.  Is that what you are trying to do with the cgi file.

    Usually you would use a .js file.

    0
     

    Author Comment

    by:seahawk9
    In the documentation there is a parameter called 'first'
    by manually changing that we get a variety of ads coming back. You can see this yourself in a browser:
    http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=3

    Copy and paste the whole thing in there and change the value for first from 3 to another number and you will see the ad change.

    I am trying to pull that 468x60 ad back into my asp page. That's why I am using a javascript call with the src attribute. I admit to not knowing very much about javascript. But it works in the code with the first parameter hard coded, I just want to vary that each time I go and get it again. It doesn't even matter to me if it is 1,2,3,4,5. It just needs to be different every five ads, thus tying it to the mod= 5 portion of the code.

    0
     
    LVL 13

    Accepted Solution

    by:
    you can use the asp variables in javascript
    type mismatch error is
    you forgot "=" sign

    <SCRIPT language="JavaScript" SRC="http://context3.kanoodle.com/cgi-bin/context.cgi?id=81257987&db=context&query=small business:office equipment&cgroup=0030bizequip&format=468x60&first=<%=irsSearchCount%>"></SCRIPT>

    you can use asp generated codes in your banner

    also
    you can use <iframe>your banner</iframe>
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now