-2

I want to dynamically add a div on a webpage that receives traffic from Google. A lot of that traffic comes from people searching some brands.

The idea is to identify the url referer and see if it contains any of the brands and say this using jquery: if the referer contains this brand-keyword then add a div saying:

'Brand'? Get free quotes for 'Brand'

How can i do that?

lets assume the keywords for the brands are:

brand1, brand2, brand3, brand4

sebas
  • 722
  • 1
  • 6
  • 21

1 Answers1

1

If you want to go ahead with this regardless of @Rob W's comment, you could do the following:

var arr = [ "one", "two", "three", "four", "five" ];
var referrer = "the number is three";

$.each(arr, function(){

    if (referrer.indexOf(this) >= 0){
        $('#ID_OF_YOUR_DIV').html(this + '? Get free quotes for ' + this + '!');
        return false;
    }

});​

Where arr is the list of words you are searching for, and referrer is the string you're looking in.

Example: http://jsfiddle.net/dS4r3/1/

Dan Ellis
  • 5,537
  • 8
  • 47
  • 73
  • Thanks for your code but how would u replace that alert with a div that contains the keyword? thats what i want to do. In your example it should insert a div that says: "Three? Get free quotes for Three!" – sebas Mar 09 '12 at 18:22
  • And i would have to replace var referrer = "the number is three"; with var referrer = document.referrer; right? – sebas Mar 09 '12 at 18:34
  • Thanks! Last question is: how do u insert the div (not just the content) Becuase im adding a background-color to it so when the referer doesnt contain one of the keywords if it would have the blank div on the page and im trying to avoid that by inserting the whole div instead of just the inner html content – sebas Mar 09 '12 at 21:02
  • It depends where you want to put it. If you have a container div that you want to add the new div to, you could use the `.html` function (http://api.jquery.com/html/), this will replace the contents of the target div with the new content. Another option would be to use the `.append` function (http://api.jquery.com/append/) which will add your new div as the last child of your container div. – Dan Ellis Mar 09 '12 at 21:10
  • Thanks again, i ended up using .show() like this: http://jsfiddle.net/dS4r3/24/?q=hsbc – sebas Mar 09 '12 at 21:31