Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting random number into script call 1

Status
Not open for further replies.

cheapeats

Programmer
Aug 25, 2005
6
CA
Hi,

I'm attempting to setup a bit of javascript to randomly display one of a set of websurveyor's polls I have configured.

The issue I'm having is that I don't know how to get the random number into the invocation of the script.

Here's what I mean, the normal way to display a single websurveyor poll say poll_id = 7448 is:
<script type="text/javascript" src=" language="javascript"></script>

I would like to substitute 7448 with a number randomly selected from the set of poll_ids I have created.

I know how to get that number, I just don't know how to insert it into the "<script..." statement, or of any other way to invoke the src file.

Any help would be greatly appreciated.
 
Perhaps you could tell us more about your random number. If generated server-side, you could simply insert it into your markup. If client-side, that's a different matter.

So, as you can see, we'd need to know more to give you an accurate answer.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
No problem...

I am generating the number client-side. The server-side is - a company that hosts surveys and polls.

I've asked them to help me, but their core business is surveying, so while they offer excellent poll-hosting they don't offer a way to deliver them randomly. (which is why I'm attempting a DIY solution.)

I'm also inserting this into a Typepad blog ( so I have limited (aka no) control over the "head" area of the code. So I'm looking for a self contained snippet of code to do this.

So far I have

--- Random poll_id from valid set --

<SCRIPT LANGUAGE="JavaScript">
survey= new Array(5);
survey[0] = "3608";
survey[1] = "3629";
survey[2] = "3817";
survey[3] = "5447";
survey[4] = "7448";
index = Math.floor(Math.random() * survey.length);
</SCRIPT>
---

But I don't know how to get survey[index] to replace 3608 in this line... or any equivalent way of doing it.

----
<script language="javascript" type="text/javascript" src="</script>
-----
 
Code:
<SCRIPT LANGUAGE="JavaScript">
   survey= new Array(5);
   survey[0] = "3608";
   survey[1] = "3629";
   survey[2] = "3817";
   survey[3] = "5447";
   survey[4] = "7448";
index = Math.floor(Math.random() * survey.length);
document.write("<script language='javascript' type='text/javascript' src='[URL unfurl="true"]http://desktop.websurveyor.net/wspoll.aspx?wsp_pollid="[/URL] + survey[index] + "'><\/script>");
</SCRIPT>


vlad
 
I have a doubt about this.

Wouldn't the script be included when the page loads? If so, the further changes wouldn't be reflected.

Cheers,
Dian
 
Hi,

vlad solutions works perfectly.

thanks everyone!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top