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 foreach
statement 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.