• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javascript url replacing without the protocol

 
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

i need to replace the url of the window to a different string URL which user has entered, for example i need to have

window.location.replace("www.javaranch.com");

but while doing this, the script appends it to the present URL and doesn't open the correct URL, i need to replace the whole URL even if the user entered string doesn't have a protocol. How to achieve that ? please help.

thanks in advance!!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on JSP.

For more information, please read this.

This post has been moved to a more appropriate forum.

 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"www.foo.com" is a relative path, not an absolute path. So it will be relative to the current location. You need to add at least the double shash to make it relative to the current protocol, thus: "//www.foo.com". But better is to take the full protocol in it, otherwise some websites will give an error if the currently used protocol isn't supported.
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this didn't work in IE6
actually the problem is user can enter any protocol in the box or decide not to enter the protocol, either way we have to only open a window passing the complete url as the user has entered, it doesn't matter if the url doesn't open a valid page, but the url in the address bar should be exactly the same as the user has entered....

is there any other way to achieve this. thanks.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Detect if the user has entered a protocol or not and append it if the user hasn't.
Fairly easy to do with indexOf or maybe a regex match.
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the fast response,

i have tried this also, but we have to check for all available protocols example (http://, https://, ftp:// etc... ) and if there is no protocol, we dont have the authority to enter a protocol of our own choice
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i have tried this also, but we have to check for all available protocols example (http://, https://, ftp:// etc... )


Then just check with regexp for something like "^[a-z]{3,5}://".

and if there is no protocol, we dont have the authority to enter a protocol of our own choice


Then it stops here. Edit: you could also pick the current protocol and append it, not sure if that is allowable.
reply
    Bookmark Topic Watch Topic
  • New Topic