View Full Version : Randomly generated URL + new window = problem... hard to summarize.

02-15-2007, 02:28 AM
Hi! Simplified code highlighting the important parts for my problem:

<script type="text/javascript">
var random_sequence = '<a href="just/a/regular/path.htm?' + generate_digit() + generate_digit() + generate_digit() + '">This is a link.</a>';

function generate_digit(){

- Here goes a function that returns a random number or letter -


Explanation: I'm creating a link to 'just/a/regular/path.htm?[three random digits go here]'. On that page, another Javascript takes the random digits to affect some of its content.

The reason I'm creating the random sequence on this page instead of on the next one, is because I want the new page to have a URL that will give exactly the same result every time, so I can't use any random functions on that page. This is important, a copy + paste of the URL must generate the same content every time, even on a different computer at a different time.

The way I do it here works fine mostly, but there is one little problem that may confuse the user. If someone clicks this auto generated link and opens it in a new tab or window, the same link will be left in the background. If they click this link again later it would lead them to an identical page, which they may not have expected. This could cause some problems if the don't notice it.

How do I prevent that? I need some way of generating the random sequence on the click instead of on load. A regular onclick and then a redirect is bad cause it breaks all control over the link. The only way that I can see right now is to send them to a dummy page that only generates the URL and then quickly redirects the user there, but that's also pretty ugly, and you wouldn't be able to right click > copy link either.

I would prefer it if I could write the <a> tag normally in HTML and just generate the actual URL on the fly, that would solve my problem, but href="javascript:generate_URL()" doesn't seem to accept returns. :(

Any ideas?

02-15-2007, 03:40 PM
How about something like...

function goUrl(url)
url = url+"search?q=hi there"; // make your random stuff here


<a href="http://www.google.com" onclick="goUrl(this.href);return false;">linky</a>

02-15-2007, 07:38 PM
Thanks for the reply. Good idea to use the DOM to access the href on the fly! Haven't tried it but I suspect that still wouldn't work very well with a right click + copy link?

'Cause apart from that little thing, I actually solved it by simply making this a regular link to the htm file, then having a script there generate the three digit code and "reload" the page, but only if the extra code is missing (obviously, otherwise it would just keep reloading). Works well.

But now I'm thinking that maybe your idea could be adapted to trigger the script on mouse over instead? In that case, everytime the link is hoovered, it will have a new URL.

I'm not very familiar with using the DOM but I will look into it and see if/how it could be done. Thanks!

02-15-2007, 09:06 PM
I'm curious as to why you're doing this with javascript at all.
Is there a reason you don't just use a server-side language to dynamically generate all urls before the page is even written to the client?

02-21-2007, 12:54 AM
Well, that would only make the problem even worse, since it would be even harder to generate a new URL for each new click from the same page.

Plus, I only know very little server side programming, and I'm not really keen on learning for such a small task.