'Change url > refresh' instead of 'refresh > change url' - javascript

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

Related

How to trigger slideToggle(); when a visitor visits another page?

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');
}
});

How to use information from the URL to click on specific button?

I'm working on a group project for a class, and we have a webpage that is split into different tabs, so it is only one webpage, but appears to be different pages using Jquery so the page doesn't have to reload when switching between tabs. The problem I am having is that one of the tabs has a form to get information from the user, then after the user clicks the submit button, the info is sent to the database using php, causing the page to reload. Then depending on if the information was successfully sent to the database, there will be either "success" or "invalid" appended to the end of the URL. If the user submits this form, we want them to automatically come back to this tab on the reload, and I have tried doing this by using a script like this:
<script>
document.getElementById("baseTab").click();
window.onload = function() {
var theurl = document.location.href;
if (theurl.includes("success") || theurl.includes("invalid") {
document.getElementById("infoTab").click();
}
};
</script>
The baseTab is the tab we want to load whenever someone first loads the webpage, unless they have just submitted the form on the infoTab page. This code structure works on a simple test webpage I run on my computer, but when I try to push it to our project repository, it will only do the "baseTab".click, and not click the "infoTab" button even if theurl includes "success" or "invalid". I tried doing it without the window.onload(), but that doesn't work either. Also, if I do
if (theurl.includes("success") || theurl.includes("invalid") {
document.getElementById("infoTab").click();
}
else {
document.getElementById("baseTab").click();
}
then neither of the buttons get clicked. If their is an easier way to do this or you see where I am going wrong, any help would be appreciated. Thanks!

How to redirect URL when php get string is in current url

Here is my HTML button:
<button class="button-brightgreen" onclick="cancelbutton();">Cancel</button>
Here is my JavaScript redirect:
function cancelbutton() {
var cancelURL = 'http://localhost:51937/php/searchUsers.php';
$(location).attr('href', cancelURL);
}
This is on my editUser.php page. This code works fine for me, except when there is a php GET string in the current URL such as ?user_id=19
When there is a GET in the URL and I click the cancel button, it takes me back to the same page (editUser.php) and displays ""
How can I get the redirect to work when there is a GET in the current URL?
You can check if the string user_id is present in the current URL, if present then redirect.
if (window.location.href.indexOf('user_id=') > -1) {
window.location.href = 'newURL';
}
I figured my problem out. The button was inside a form and when clicked, it would submit the form and the "" was coming from my PHP page. Taking the button out of the form, fixed my problem.

JS onclick="history.go(-1) only if under my domain

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>

Conditional refreshing page - how to set a referrer?

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.

Categories