0

I am trying to replace the visited URL with another URL whenever certain search terms are used.

However browsing bing.com for the term "foobar" results in the URL bing.com/www.google.com.au instead of just google.com.au.

I have tried using both location.href and location.replace():

var OldUrl = location.href
var NewUrl = "www.google.com.au";

var arr = ["foobar"];
for (var i = 0, len = arr.length; i < len; ++i) {
    if (OldUrl.indexOf(arr[i]) != -1) {
        // str contains arr[i]
        location.href = NewUrl; 
        //location.replace(NewUrl);
        found = true;
        break;
    }
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
yoshiserry
  • 20,175
  • 35
  • 77
  • 104
  • You must prefix your url with a protocol (such as `http://` or `https://`) otherwise it is handled like a relative url. – CollinD Feb 24 '16 at 22:43
  • 1
    @CollinD Thanks appending the protocol worked. If you write the answer I will accept. – yoshiserry Feb 24 '16 at 22:48

1 Answers1

1

URLs are treated as relative unless prefixed with a protocol. To solve your issue in particular, I'd suggest

var NewUrl = 'https://www.google.com.au'
CollinD
  • 7,304
  • 2
  • 22
  • 45