0

I wrote a small script to recreate the Chrome address bar, wherein my code checks an input for any domain extension(.com, .edu etc.) and sets a boolean flag to true if an extension is found. It then checks the flag and based on the result opens the website or sends it to google as a query. Additionally, if it is a website, it checks if the string contains http:// and www. and if not, adds it to the string before using Window.Open() to open the target.

What's wrong here?

function openSite(){
     var domain_extensions = [".aero", ".asia", "...All Other Extensions...", ".zr", ".zw"];

        var isSite = false;

        var userIn = document.getElementById('in_field').value; //Retrieves Textbox code

        for (var i=0; i < domain_extensions.length; i++)
            if (userIn.search(domain_extensions[i]) !==-1)
                isSite = true;
                        //Checks against the array of extensions

        if (isSite === true){
            if (userIn.search("http://") === -1 || userIn.search("https://") === -1)
                {if(userIn.search("www.") === -1)
                    userIn = "http://www." + userIn;
                 else
                    userIn = "http://" + userIn;                        
                }

                window.open(userIn, '_blank');
                //if extension is found, open website
                        //if qualifier http:// or https:// and/or www. not found, append and open website               
            }

        else{
                var str = encodeURI("http://www.google.com/search?q=" + userIn);
                window.open(str, '_blank');

            } //Searches query for common extensions; if not found search google
    }
12
  • 2
    What's not happening? And why is JavaScript's "truthiness" relevant? Commented Jul 18, 2013 at 19:48
  • 1
    Why are you using http:backslashbackslash and not http://? Also, you may want to use break; in your for loop, although I don't think that would fix anything Commented Jul 18, 2013 at 19:49
  • If you add a break, be sure to also add curly braces... Commented Jul 18, 2013 at 19:51
  • @Ian Oh damn! That fixed a bit of my issue. Now websites work. Commented Jul 18, 2013 at 20:01
  • @contactmatt Even when I type a non-site in, the truth block still executes. Commented Jul 18, 2013 at 20:02

1 Answer 1

1

This is a problem, I believe, with your usage of the search function. This function takes a regular expression as its argument. The . character is special in regex, and matches any character.

For example:

var test = "blasdfahsadfcomasdfasd";
console.log(test.search(".com")); // prints 11

Prepend the . with a backslash in order to override this behavior:

var test = "blasdfahsadfcomasdfasd";
console.log(test.search("\\.com")); // prints -1

Additionally, if you want to check only at the end of a string, add a $ symbol at the end of the strings like so:

var test = "blasdfahsadf.comasdfasd";
console.log(test.search("\\.com$")); // prints -1; prints 12 w/o the $
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.