Reputation: 45
I have a small loading animation that I want to run on my Home page when a user is coming from outside my domain, but when coming from another page inside my domain I don't want it to show.
I have tried using document.domain
and document.referrer.split...
to get the previous domain, and run an if-command but can't seem to make it work.
Edit:
I tried using the if
-command and document.referrer.split...
-command, again as an answerer stated but the animation still always shows and now it never stops as well...
<head>
<body>
<div class="loader"></div>
<style>.loader {
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 9999;
background: url('http://bosonprotein.com/onewebmedia/Magiska%20Bollar.gif') 50% 50% no-repeat rgb(0,0,0);
background-size: 10%;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
if(document.referrer.split('/')[2]!=location.hostname){
$(window).load(function() {
setTimeout(function(){
$('.loader').fadeOut('slow', function () {
});
},1000);
})
}
else{
}
</script>
</body>
</head>
Upvotes: 1
Views: 76
Reputation: 8380
You can parse the referrer url using this technique:
var parser = document.createElement('a');
parser.href = document.referrer;
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host;
parser.host
will return the domain from the referrer.
Upvotes: 1
Reputation: 2115
Instead of detect the domain, you could use a cookie and jQuery, maybe something like this.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="welcome">
<div>
<h1>Hello..!<br> Welcome to ABC
<br>
</h1>
<h2>We wish you a Great Day..!</h2>
<br>
<h2><a id="close-welcome" href="#">Thank you.. and please take me to the website</a> </h2>
</div>
</div>
$(document).ready(function() {
if ($.cookie('noShowWelcome')) $('.welcome').hide();
else {
$("#close-welcome").click(function() {
$(".welcome").fadeOut(1000);
$.cookie('noShowWelcome', true);
});
}
});
This will load it per session only.
Upvotes: 0
Reputation: 32
document.refferer.split()
is the right solution for solve your problem.
According to the documentation, document.refferer
return the URI of the previous page.
Compare the splitted URI with the location.host
like the following code snippet:
if(document.referrer.split('/')[2]!=location.hostname){
//I came from another domain
}else{
//I came from this domain
}
I used this solution in different projects and it works.
Upvotes: 0