Josh Murray
Josh Murray

Reputation: 637

redirect to page depending on the url

I am trying to pull in the url (including path) and set it to a variable with jQuery, read the variable and see if it matches anything.

I can see it work because I can see the outputs in the console, but the body won't change colour.

NOTE: Yes, I have opened the file with my file called facebook.php (so the background should go blue)

My code is below:

function containsStrURL() {
    console.info("redirect.jm.js: Started!");


    var str = window.location.pathname;

    var res = str.match(/facebook/g);
    if (res === "facebook") {
        $(document.body).css({
            'background-color': '#3E5B98'
        });
    } else {
        res = srt.match(/twitter/g);
        if (res === "twitter") {
            $(document.body).css({
                'background-color': '#4DA7DE'
            });
        }
    }


    console.log("Redirecting to " + res);
}

containsStrURL();

I have also tried $('body').css(...); but never worked.

There isn't anything in the HTML document to be covering the page, if that makes sense.

Any help is appreciated.

Upvotes: 1

Views: 39

Answers (1)

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

1st: you have to use $('body').css

you can try

if (str.toLowerCase().indexOf("facebook") >= 0){
        $('body').css({
            'background-color': '#3E5B98'
         });
  }

Upvotes: 1

Related Questions