1

slightly related to this question here.

My problem:

I am writing a bit of script that will read the referring URL of a page and check to see where it comes from i.e. Google, Bing etc. Now I know I can just do a document.referrer.search('google'); but I don't want to do it like this as it makes sense to read the values from an array or object. I've created an object which has the term that I am searching for and the property name as follows:

var searchProviders = {
  "google": "google.com",
  "bing": "bing.com",
  "msn": "search.msn",
  "yahoo": "yahoo.co",
  "mywebsearch": "mywebsearch.com",
  "aol": "search.aol.co",
  "baidu": "baidu.co",
  "yandex": "yandex.com"
};

Now, what I want to know is can I iterate through this object and do a document.referrer.search(searchProviders[0]) and then return the property name? so for example if it contains, google.com I want it to return google? The reason I want this term to be returned (and not the search term) is because this value needs to be used later on.

I remember reading in the question posted above that you can only use a foreachstatement to iterate through an object - is there another way for me to do this? Of course if I could use a foreach statement, but I want the statement to break if it finds the correct value (hence why I want to use an if).

So I'm a little stuck as to what's the best approach. Any help would be appreciated, hope I've explained everything well - if not please tell me and I will update my question.

Community
  • 1
  • 1
zik
  • 2,745
  • 8
  • 35
  • 59

3 Answers3

3

You should be able to iterate like this:

for (var key in searchProviders) {
    if(!searchProviders.hasOwnProperty(key)) { continue; }
    var URL = searchProviders[key];
    if (document.referrer === URL) {
      return key;
    }
}
Jason T Featheringham
  • 3,188
  • 23
  • 32
Morten Siebuhr
  • 5,812
  • 4
  • 27
  • 41
1

One way;

var SearchProviders = {
    Tags : {
        "google":"google.com",
        "bing":"bing.com",
        "msn":"search.msn",
        "yahoo":"yahoo.co",
        "mywebsearch":"mywebsearch.com",
        "aol":"search.aol.co",
        "baidu":"baidu.co",
        "yandex":"yandex.com"
    },
    lookup : function(tag, fuzzy) {
        tag = tag.toLowerCase();
        for (var k in this.Tags) {
            if (this.Tags[k] === tag || (fuzzy && tag.indexOf(this.Tags[k]) >= 0))
                return k;
        }
        return null;
    }
};

var name = SearchProviders.lookup("google.com")
name && alert(name);

var name = SearchProviders.lookup("blah.google.com", true)
name && alert(name);
Alex K.
  • 159,548
  • 29
  • 245
  • 267
0

It's possible that you're using keys where you would be more comfortable using values. consider using an array of objects:

var searchProviders = [{
    name: "google",
    url: "google.com"
    },{
    name:"bing",
    url: "bing.com"}];
graphicdivine
  • 10,539
  • 7
  • 30
  • 58
  • Yes this may be easier, but then how can I return just the name? can I just do searchProviders[0, name];? – zik Dec 14 '11 at 12:29