The moose likes HTML, CSS and JavaScript and the fly likes Random URL and Redirection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Random URL and Redirection" Watch "Random URL and Redirection" New topic

Random URL and Redirection

Sagar Kumbhar

Joined: Oct 27, 2007
Posts: 17
I have an Iframe or just a frame and I want it to show a random url each time,
is there any script to do so ?

And another one;
I have a page with 2 frames, the first one has some content and the second
shows the url "", the user enters a search term and
clicks "search".
What I want is that when he clicks "search" the second frame redirects him to the "results page" and the first frame redirect to another page "page1".

Like, on second frame exit, first frame redirect is activated.
Any way I can do so (like onExit or onUnload)


Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 471

Hello Sagar,

You can simply put the URLs that you want in an array, and use Math.random() to generate a random number (which is between 0 & 1, so you have to multiply it by the array's length and use Math.floor() on it). Use the random number as the array's index to get a random URL of the ones stored in the array, and use it as the source for the frame (or the iframe).

As for the second question, it's a little tricky. The simplest google search url is If the search term is made up of several words, they are separated by a + sign instead of spaces. Of course you have to take care of the special characters that google use marks to move the search to a specific direction (like the + sign to assure that the following word is present or the - sign to make sure that the following word is not present, etc). This can be done by taking the search term, splitting it into spaces, and using the escape() method on each part of the search term to generate the search URL.

[ February 12, 2008: Message edited by: Alaa Nassef ]
[ February 12, 2008: Message edited by: Alaa Nassef ]

Visit my blog:
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 471

Hello again Sagar,

There are a couple of problems that I didn't mention. The smaller of them, which is not actually a real problem, is that google uses other attributes in the URL to specify the browser, the encoding, and other stuff. This is not a real problem, since you can get some of the attributes yourself, and if you didn't specify them, the defaults will not cause any trouble in the normal cases.

The second problem is the real problem, and that is you want the first frame to point at, and if you do that, you'll not be able to make the search button make the search be done in the second frame; except of course if you change the code. You can either create your own search page, and use it in the first frame, or you can parse the html of, and change the action of the submit button to make it display the search page in the second frame.
I agree. Here's the link:
subject: Random URL and Redirection
It's not a secret anymore!