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>
Related
How can I have a link go to a page as expected for logged in users only? If a user is not logged in I would like a popup login form displayed instead.
PS-I am new to JS and StackOverflow so please be kind and if my future questions should be set up differently, please let me know.
I have tried a lot of different methods and I am able to get the script to display alerts, but not the desired actions.
This is the link HTML that should go to the defined href value if user is logged in and display the pop up if logged out.
Go To New Page
Here is the script I am using and could use help with actions that need to happen.
<script>
function lockedContent(){
if (document.body.classList.contains("logged-in")){
//go to link defined in the href in the <a>
} else {
//display login popup
}
}
</script>
I would like the link to go to the desired url in the href for logged in users and to display a popup for users that are not logged in. So far I can't seem to get either to happen.
You can redirect using window.location. After proper authorization you can add the class logged-in to the body and then redirect
if (document.body.classList.contains("logged-in")) {
// only after proper authorization
window.location.href = 'url of the page'
} else {
// code to call modal display function
}
You can use a button instead and check if user is logged in or not based on that change window.location
let loggedIn = true
function lockedContent(){
if(loggedIn)
{
window.location.href = 'http://www.google.com'
console.log(window.location.href)
}
}
<button onclick="lockedContent()" id="bookOne">Go To New Page</button>
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 try to find Q&A related to my question. But I couldn't find. I have created a code where if user click double or multi click at single link then he will redirect to error page.
My Code is
<script>
var doubleClick = false;
function myWillPageRedirect() {
if(!doubleClick) {
doubleClick = true;
window.open("errorPage.html");
}
}
</script>
But this is not working. Why?
I want to create a secure page generally we can see in some bank or financial website.
I want to create 'if user click window back button then he will redirect to error page.' OR if if click refresh button then same action will perform and redirect to error page. ..
If this all code can make in Linux server backend code. It would be great.
Please help. I am not saying to some create code for me , I am asking for help to some correct my code above or bit extra help to make them secure.
Thank you .
HTML:
Double-click me
JS:
if(performance.navigation){ //Check if the page was refreshed
if( performance.navigation.type === performance.navigation.TYPE_RELOAD ){
myWillPageRedirect();
}
}
function myWillPageRedirect() {
window.open("errorPage.html");
}
window.onhashchange = function() { //Check if the url has changed (in case of pressing back button)
myWillPageRedirect();
}
More infos about Performance.navigation
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 have use modal for user sign up and its working correctly when user click on register link the modal popup load perfectly now i am making comment box where user can comment on pictures and if user is not login i want to load the same popup modal which is bind with register link some how i am successful but the error is that the load() function load the entire page for me on the same page i am totally confuse. here is my coding .
$("#button").click(function(){
var user=$("#username").val();
var message=$("#form-s-t").val();
if(user != ""){
if(message != ""){
$.ajax({
type:"post",
url:"retriveing.php",
data:"name="+name+"&message="+message+"&action=addcomment",
success:function(data){
showComment();
}
});
$("#form-s-t").val('');
}
else {alert("you can not post empty comments");}
}
Here in else portion i am loading the popup.
else{
$('.topopup').load("#toPopup");
}
});
});
The popup which loads with register link is this.
User which is not login want to comment it load the page twicly and oppositly as shown in picture.
There is no need of load() function if the model resides in the same page . try to trigger the register link which can be done like that.
Remove the load function in your else portion and just write the code I have give below.
$('.topopup').trigger("click");
.topopup
is a id of register link and you want to load this popup when user is not sign in and want to comment. I hope it will work for you. if the model resides in the same page.
From you screenshot, you send the wrong argument to the server.
$('.topopup').load("#toPopup");
// you just load "/#toPopup" page from the server
// and the server return a new page to you
// then jQuery update '.topopup' with the new
// page
You are loading an anchor, so the same page is getting injected into the elements with class .topopup. Just check your register button's onclick function and use else { thatFunction(); } if you want to recreate the same behaviour. [Also try using retrieving.php as a filename.]
To clarify: load("#toPopup") is equiv. to load(currentURL+"#toPopup"). First parameter is an URL string, not a selector string.