2

Have a look at the site below, will delete it later:-

[Career page][1]

On this page, if I come from other website like Facebook/ Linked in, it should look like this:-

![Image 1][2]

And If I visit from the same site, it should be like below:-

here is my JS code related to that. Please suggest what to do:-

function pageLoad() {
        $("#careerdiv").accordion({              
            collapsible: true,
            autoHeight: false,
            active: false
        });

        $("a#various15").fancybox({
            'width': 720,
            'height': 390,
            'autoScale': false,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'type': 'iframe',
            'speedIn': 600,
            'speedOut': 400,
            'overlayShow': true,
            'overlayOpacity': 0.8,
            'overlayColor': '#000',
            'padding': '0px',
            'onComplete': function () { $('.closer').click(function () { parent.$.fancybox.close(); }) }
        });
    }

Please suggest what to do

Nad
  • 4,605
  • 11
  • 71
  • 160

4 Answers4

3
if (document.referrer.indexOf('facebook.com') > -1) {
  // do something for visitors from facebook here
}
Hamza Kubba
  • 2,259
  • 14
  • 12
  • where to add this code ?? and how it open as per my first image ? – Nad Apr 23 '15 at 08:42
  • Inside your pageLoad() function, probably... as for how you control the accordion to open up automatically, you should look at the documentation for that plugin for that answer. Probably the `active` field, but I'm just guessing. – Hamza Kubba Apr 23 '15 at 08:43
  • can you edit your answer post and let me know so that I can check it on my website whether it is working or not ? – Nad Apr 23 '15 at 08:45
  • 2
    I don't understand, you want me to read the documentation for the accordion plugin for you? You can find it here: http://api.jqueryui.com/accordion/ If you don't want to put in any effort to learn, then I don't want to put in any effort to help you. – Hamza Kubba Apr 23 '15 at 08:47
  • @Nadeem there is surely an api function for that as Hamza mentions, of course you could also just call `.click()` on the element that opens the accordion – Wesley Smith Apr 23 '15 at 08:52
2

You can use document.referrer. Here is a contrived example:

$(document).ready(function() {
   var referrer =  document.referrer;
   if(referrer.match(/stackoverflow.com/i)){ // change this to the name of your site
     $('#targetDiv').show(); // change this line as needed for you actual page
   }
   else{     
     $('#targetDiv2').show(); // remove this line for you actual page
     // $('.ui-accordion-header').eq(0).click(); // uncomment this line for your actual page
   }
});
.none{
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="targetDiv" class="none">You see this, so you came from SO</div>
<div id="targetDiv2" class="none">You see this, so you came from a site other than SO</div>
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • thanks for the reply, And How to open the first `div` of the page if it comes from other website rather than the same website ? – Nad Apr 23 '15 at 08:44
2

You just have to check if referrer is from fb || li and open accordion number 1 I guess:

if( document.referer ){
    $("#careerdiv").accordion({              
        collapsible: true,
        autoHeight: false,
        active: 1
} else {
    $("#careerdiv").accordion({              
        collapsible: true,
        autoHeight: false,
        active: false
    });     
}
Kresimir Pendic
  • 3,597
  • 1
  • 21
  • 28
  • Yes, I want that. but in your both condition is same, I want like first Image if it is `if( document.referrer.indexOf('facebook.com') > -1 || document.referrer.indexOf('linkedin.com') > -1 ){` – Nad Apr 23 '15 at 08:47
  • you can change if statement to this `if( document.referer != "" ) ..` and then show `active: 1` is that what you want? – Kresimir Pendic Apr 23 '15 at 08:55
  • Yes, but it is opening the accordion if I visit from the same site – Nad Apr 23 '15 at 09:09
  • referer is not gonna change if you go to (click on logo) homepage and return to that page.. it is persistent while the session is open.. try to open page in incognito window – Kresimir Pendic Apr 23 '15 at 09:20
  • Tried from their, it is still opening if I visit from the same site – Nad Apr 23 '15 at 09:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76029/discussion-between-oserk-and-nadeem). – Kresimir Pendic Apr 23 '15 at 09:39
1

Can you please change the following of your code:

$("#careerdiv").accordion({              
    collapsible: true,
    autoHeight: false,
    active: false
});

with the following (of which host check taken from here):

if( document.referrer.indexOf(location.protocol + "//" + location.host) === 0){ 
                $("#careerdiv").accordion({              
                    collapsible: true,
                    autoHeight: false,
                    active: false
                });
            } else{
                $("#careerdiv").accordion({              
                    collapsible: true,
                    autoHeight: false,
                    active: 0
                });
            }

which checks if the referrer is you own host and activates the first accordion which has the index 0 (in zero based index). Remember to test it on the server.

Community
  • 1
  • 1
wooer
  • 1,677
  • 2
  • 16
  • 27