Fadi Walah
Fadi Walah

Reputation: 19

Check / find in url if the there're any word of the array

Hi guys i have make this :

$("a").click(function() {
    var x = $(this).attr("href");

    var Site_dont_go = ["#", "youtube.com"];

    var check = jQuery.inArray(x, Site_dont_go);
    if(check > -1 ){

    } else {

        window.open('goto.php?url='+x);
        return false;
    }

});

it is work fine but if the url was like (youtube.com/other_code) it's not checked

Please help me

Upvotes: 0

Views: 39

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

You need to search within the URL itself to find the domain. You can do this with indexOf:

$("a").click(function() {
    var x = $(this).attr("href");
    var Site_dont_go = ["#", "youtube.com"];
    var validUrl = true;
    $.each(Site_dont_go, function(i, item) {
        if (x.indexOf(item) != -1) {
            validUrl = false;
        }
    });

    if (!validUrl) {
        window.open('goto.php?url=' + x);
        return false;
    }
});

Note that searching for # is not a great idea though, as you will end up excluding any URL with a fragment, eg. http://www.microsoft.com/#foo

Upvotes: 5

Related Questions