MortenMoulder
MortenMoulder

Reputation: 6648

Remove characters (if found) in URL and then redirect jQuery

If an URL contains the word "hello", then remove "hello" from the URL and redirect. How is that possible? Example:

http://hello.google.com/

On page load, I want to check if "hello" is found, and then do a:

window.location.href = 'http://www.google.com';

Thanks in advance

Upvotes: 1

Views: 1058

Answers (2)

Andy
Andy

Reputation: 30135

No need for jquery here:

window.onload = function(){
    if(location.href.indexOf('hello') > -1){
        location.href = location.href.replace(/hello/,'www');
    }
};

Upvotes: 2

Barmar
Barmar

Reputation: 780724

if (url.match(/hello/)) {
    window.location.href = url.replace(/hello/, 'www');
}

Upvotes: 1

Related Questions