I want to refresh my particular page, when referrer is blank.
To do this I used this script:
if (document.referrer === "" && document.URL.match("mysite.com/page1/module1")){
window.location.reload();
}
Of course it refreshes endlessly - because after refreshing the site the referrer is blank too.
How can make it to refresh only one time?
Try this:
top.location.href = top.location.href;
Instead of this:
window.location.reload();
And it's better to use PHP to make this condition:
<?php
if(!$_SERVER['HTTP_REFERER']) {
echo '<script>top.location.href = top.location.href;</script>';
}
?>
Anyway what you are trying to do is very bad practice, because many people are blocking browser to send referers.
Related
I have written a piece of code which triggers a slideToggle() after a 2 second wait on a page.
Now, this is not something I want to show to visitors who have the intention to immediately leave (bounce) from my site.
I determined that the best time to show the div is when a visitor has landed on my page, and navigated to another page.
So right now my code is this:
<script>
jQuery(document).ready(function(){
setTimeout(
function()
{
jQuery(".slide-div").slideToggle();
}, 2000);
});
</script>
Is there a way to listen to a visitor's behavior and trigger a function (slideToggle() in this case) when this visitor has navigated to another page?
This way, I can show my div to people who have navigated the website.
For future reference, I ended up using PHP to solve this problem.
Explanation: Each time a person navigates (or refreshes) the page, +1 is added to the $_SESSION['count'] variable. Then in footer.php I wrote a simple if else statement to determine wether my function should appear or not. wonderplugin is just a plug-in that checks if a visitor is on mobile or not.
Header.php:
<?php
session_start();
if (!isset($_SESSION['count']))
$_SESSION['count'] = 1;
else
$_SESSION['count']++;
?>
Footer.php:
<?php
$count = $_SESSION['count'];
if ($count == 1) {
//
}
elseif ($count >= 1 && $count <= 100) {
if (wonderplugin_is_device('iPhone,iPod,Android'))
echo get_function_mobile();
else
echo get_function();
}
?>
If you want a count of how many pages your users have been on you would need to hold onto a variable somewhere that is not on your page that you could increment with a page count. That may be in local storage, a cookie, or in your server side code.
Another thing you could do is add query strings to links on your pages if all you want to know is that someone has navigated from another page within your site. Then, check the query string on the page load and show your div if they are coming from an internal page.
You can use Javascript and localStorage to remember the user's action :
jQuery(document).ready(function()
{
var previousAction = localStorage.getItem('user-action');
if(previousAction)
{
setTimeout(function()
{
jQuery('.slide-div').slideToggle();
}, 2000);
}
else
{
localStorage.setItem('user-action', 'new-action-spotted');
}
});
I know the title sounds a bit weird, but I didn't know how else to explain this is 1 sentence.
Basically when I press f5 to refresh (or click on the refresh button on the url bar) I want the following to happen: Change url> refresh instead of refresh > change url what it does my code right now
The problem of that is that i'm using a php mail form that puts ?resp=.. in my url bar and if that is the case my
My code:
if (performance.navigation.type == 1) {
window.location.replace("index.php#home");
}
The problem of that is that i'm using a php mail form that puts ?resp=.. in my url bar and when I reload with ?resp= in my code my javascript will open a modal with this code:
<?php
if (isset($_GET['resp'])) {
echo '<script type="text/javascript"> $("#myBtn").click(); </script>';
}
?>
A possible other solution is maybe putting && performance.navigation.type != 1 in that if statement but that didn't work.
So I am looking for 1 of those things:
Change url> refresh instead of refresh > change url
Or add something to the if statement that makes the modal not open if its on a refresh
i am getting nuts with something .
What i am trying to do is to set a cookie when loading a certain page so that when being on another page and the cookies has been set, the page is refreshed.
My condition tests do not react as how i expect it .
here is where i am at :
$( window ).load(function() {
var page=$(".listing_voyage");
//alert('page:'+page.length);
//alert('cookie:'+getCookie('refresh'));
if (getCookie('refresh') && page.length )
{
window.location.reload(true);
SetCookie("refresh",false);
alert('i am reloading');
}
});
var page_produit=$("#page_produit");
if (page_produit.length)
{
SetCookie("refresh",true);
}
Once you reload the page, the javascript is lost, and everything starts over
window.location.reload(true); // reloads page
SetCookie("refresh",false); // and this is not executed, as the page reloaded
alert('i am reloading');
You'll have to set the cookie before reloading
SetCookie("refresh",false);
window.location.reload(true);
But why exactly are you reloading the page to begin with, it seems like a strange thing to do ?
I have a Joomla site, and I have the following problem, I need a "Back to search page" function on my product details page, and I am using this code after some changes according to replies to my initian question:
<br/><?php echo JText::_('VOLTAR'); ?>
Now, if a Visitor comes from another site directly to my product page and click this, he will be redirected to homepage of my site, and this is ok, but if I in a search page of my site, click on a product page and then click on the back to search link, the visitor is also redirected to my homepage, which is not good, it should be redirected to previous page, which was his own search page.
Is there any way to modify this code in order to accomplish something like:
if visitor comes from my search page or from anywhere in my site, by clicking this he will be redirected to the previous page, and if a visitor came from outside of my site, by clicking this he will be redirected to my homepage?
You can use document.referrer and compare it to window.location.host.
if (document.referrer.split('/')[2] === window.location.host)
if (document.referrer.indexOf(window.location.host) !== -1)
So your HTML will look like this:
<?php echo JText::_('VOLTAR'); ?>
Adding branching logic into an inline click handler gets messy. If you can move this to a function and reference it it will be far more readable.
if(document.referrer.indexOf('mysite.com') >= 0) {
history.go(-1);
}
else {
window.location.href = 'myHomePageUrl'; // this might just be '/' of your site
}
Edit:
If you are not concerned about adding names to the pages global scope you can create a function in a script tag immediately before the link you are creating:
<script>
function backClick() {
// above conditional goes here.
return false;
}
</script>
<br/><?php echo JText::_('VOLTAR'); ?>
I have tried some of the codes above and I needed to make some changes to make it work for me.
remove href=""
"window.location.href" must have
http://
function backClick() {
if (document.referrer.indexOf(window.location.host) !== -1) {
history.go(-1); return false;
}
else { window.location.href = 'https://stackoverflow.com'; }
}
<a onclick="backClick()">go back</a>
What I am trying to do is redirect a user based on their referring url to a promo page. In the script below if someone comes from a referring url from “mydomainsite.com” they will be sent to “mydomainsite.com/promo.html” When I have the script below in the page “mydomainsite.com/promo.html” and it comes from a refer of “mydomainsite.com” it seems to loop or continue to load the page and it never loads the page “mydomainsite.com/promo.html” The script has to be in the “mydomainsite.com/promo.html,” do to the page being a promo page and everyone can’t have access to the page. I assume this is due to indexOf and it checks for “mydomainsite.com” then executes. Is there away to fix this?
<script language="JavaScript">
if (document.referrer.indexOf('mydomainsite.com') > -1)
location.href='http://mydomainsite.com/promo.html';
else
location.href='http://notfrommydomainsite.com';
</script>
As has been mentioned in the comments the referrer data is not reliable, however if you do want to pursue this...
The mqost likely cause of the issue you have is that when arriving at the redirected promo.html it finds the referrer is from mydomainsite.com so it simply goes to itself creating am infinite loop.
You will need to test for that and avoid the loop by checking that location.pathname is not promo.html :
if (document.referrer.indexOf ('mydomainsite.com') > -1 &&
location.pathname !== 'promo.html')
location.href = 'http://mydomainsite.com/promo.html';