Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    tbr
    tbr is offline
    New Coder
    Join Date
    Sep 2005
    Location
    sweden
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Randomly generated URL + new window = problem... hard to summarize.

    Hi! Simplified code highlighting the important parts for my problem:

    Code:
    [...]
    <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>';
    document.write(random_sequence);
    
    function generate_digit(){
    
    - Here goes a function that returns a random number or letter -
    
    }
    </script>
    [...]
    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?

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    How about something like...

    Code:
    <script>
    function goUrl(url)
    {
    url = url+"search?q=hi there"; // make your random stuff here
    
    window.document.location.href=url;
    }
    </script>
    </head>
    
    <body>
    <a href="http://www.google.com" onclick="goUrl(this.href);return false;">linky</a>
    </body>

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    tbr
    tbr is offline
    New Coder
    Join Date
    Sep 2005
    Location
    sweden
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    tbr
    tbr is offline
    New Coder
    Join Date
    Sep 2005
    Location
    sweden
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

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