• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Random URL and Redirection

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 "www.google.com", 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)


thanks
Sagar
 
Ranch Hand
Posts: 471
Mac OS X Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 www.google.com/search?q=searchTerm. 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 ]
 
Alaa Nassef
Ranch Hand
Posts: 471
Mac OS X Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 www.google.com, 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 www.google.com, and change the action of the submit button to make it display the search page in the second frame.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic