Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8174321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:25:19+00:00 2026-06-06T22:25:19+00:00

I have a setup like, My website ask the user for entering a URL.

  • 0

I have a setup like, My website ask the user for entering a URL. Sometimes user enters like http://google.com/ and other times google.com, but my application supports URL with http:// or https://,
I tried this:

var a = document.getElementById('tinput').value;
if (a.indexOf(escape('http://')) < 0 && a.indexOf(escape('https://')) < 0){
b = 'http://' + a;
document.getElementById('tinput').value = b;
}
document.getElementById("urlfrm").submit();

But this dint work. It always adds http:// to all the URL even if they contain the same. What to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:25:21+00:00Added an answer on June 6, 2026 at 10:25 pm

    Try this:

    var field = document.getElementById('tinput');
    if (!/^https?:\/\//.test(field.value)) field.value = 'http://'+field.value;
    

    A couple of points to note:

    • your current check allows for http(s):// to be anywhere in the string – you should check for it at the start only

    • in the interests of code readability, and because the only value under zero that indexOf() can return is -1, it’s better to check for that explicitly, rather than < 0

    • you’re creating a global variable (b) for no reason

    • don’t escape – as xdevel said in his comment, this will mean you check for an encoded version of the string, not the string itself

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a setup like this: http://jsfiddle.net/zzjSC/3/ and I am wondering why the surrounding
I have an array setup like the following: services = ['times', 'food', 'messages', 'share']
I have a website project and would like to setup different web.config transformation files
I have a dedicated Linux web server where my website is running like www.example.com.
I have a setup like this: <div id=container> <div id=close>Close</div> </div> Then in my
I have a setup like this. var element = $(.module-panel, this.el); $('.module-panel:not('+element+')').hide(); What I'm
I have a wizard setup like so: <asp:Wizard ID=Wizard1 runat=server DisplaySideBar=false onnextbuttonclick=OnNextButtonClick> <WizardSteps> <asp:WizardStep
I have a Drupal setup like this: Content type: Apartments Vocabulary: Areas , that
Hi everyone and couchdb pros, I have a mapping setup like such: class Product(BaseModel):
I have a lot of pages that are setup like so from an old

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.