reliable way to find site refereer - javascript

I am currently working on a task where for example an user visits www.site.com satisfying a particular condition I am supposed to make some visual transformations in the page. But this should only happen for the first time the user is shown the page. Subsequently if the user ignores the call to action and browses the site everything is normal.
To make sure that for a given session this transformation only happens once I am using document.referrer to check if it is
1. an "" string which means user might have entered the www.site.com address directly
!document.referrer.match(/www.site.com/gi); - to make sure that the user is not referred from the internal pages back again to the home page.
This works in most cases except when user gets into the check funnel the url changes to a secure one https:// and when he is referred back to site.com home page the document.referrer returns an empty string which confuses my logic a the user is entering the address in the URL
Is there any other reliable way to solve this problem. Any help is much appreciated and thank you for taking time to read my problem

function transformation(){
// transformation code
}
if(document.cookie.match(/someCookieName/) && document.location.href.match(/transformationPageURL/)){
// call the transformation
transformation();
// set session cookie
document.cookie="someCookieName=true;";
}else{
// not the url to perform transformation or the cookie is already set which means its the same session.
// but still if the user again enters the same home page url the transformation //would not appear due to the cookie being set. But the changes of normal user //visiting the website and again entering the url of home page might be less
}

Related

my redirect_uri results in an "Unable to connect" warning

My question is a follow-up to how to programmatically create an authentication/login window. One reply to that question suggested that I look at the example code in https://meta.stackexchange.com/a/293498/148310.
I implemented that code, and I am now able to get a login/authentication window. The problem now is that immediately after entering my login credentials, I am redirected to my specified redirect_uri URL address (localhost, see image), where the browser complains of "Problem loading page", "Unable to connect" error/warning (click for image), a situation that I think is terminating the execution of my code. Note that the desired access token number does appear to be attached to the end of the URL, as it should be, in spite of the browser's page-loading problems. The problem is that my code gets terminated by the browser-loading problem before it can read the token off of the redirected page.
The below snippet out of my code (also included in the example code given in link above) looks at the window's URL to determine if the browser has redirected (indicated by the presence of 'oauth/login_success' in the URL address). The code considers a redirected browser as indication of successful login/authentication of the user; the access token at the end of the URL address is ready for programmatic extraction and application. (In my case, I want the code to use the token in an API call to download a bibtex file from my Mendeley research group account). Otherwise, execution of the login/authentication process is triggered.
function getInfo()
{
if (location.pathname == "/oauth/login_success") {
processOauthPopup ();
}
else {
authorizeAndThenRunMain ();
}
}
In my code, at the end of the authorizeAndThenRunMain function, I have a call back to getInfo(), the expectation being that the code would then choose the processOathPopup function. On the contrary, the code seems to stop execution when the browser redirects to localhost. If I press the button on my ShareLatex page again to initiate another call to getInfo, I just get presented with the login / authentication screen again. The code never gets on with the business of extracting the access token (at the end of the redirected url shown in the image) and completing the desired tasks under the processOauthPopup () function.
To get to the point, my question is: how do I tell my code to ignore the fact that the redirect window has a loading error, and to just get on with extracting the token from the URL in that window and proceed executing the processOathPopup function?
To elaborate a bit more, I speculate there are 2 potential problems with my code that could be relevant to the problem of the code never selecting the processOathPopup option:
My GreaseMonkey script is run on www.sharelatex.com/projects/xxxxx, where xxxxx is specific to the user account and the document that is being edited in the account (think of Google Drive, where each document has a unique ID number). My code takes me to a api.mendeley.com/oauth/ site where I authenticate and log in, and then I am redirected to the localhost:5000/oauth/login_success link (see image). Is the fundamental problem that my redirect url points to localhost rather than back at sharelatex.com?
If so, would it matter that an "www.sharelatex.com/oauth/login_success" does not point to a real website?
If indeed "sharelatex" needs to be part of the redirect URL, does it need to be an identical URL address to the sharelatex page that I started from? Or can it be a truncated address, with the document-specific information redacted (e.g., the xxxxx stuff described above that links to a specific document in sharelatex). Having to go through the Mendeley userscript registration system to change the URL every time I wanted to use this code for a different sharelatex document would be not good. Even then, a "/oauth/login_success" tacked on to a legitimate URL will result in a redirected URL that points to a non-existent website, which I think is the problem with using the localhost redirect.
In order for location.pathname to ever equal "/oauth/login_success", wouldn't the redirect window need to load into/replace my sharelatex window from where I initially launched code execution (My GreaseMonkey code makes a button on my ShareLatex website that I use to initiate the execution of the above getInfo function).
Or is location.pathname smart enough to point to whatever tab in the browser was most recently opened (e.g, the api.mendeley.com followed by the redirected localhost URLS), rather than always pointing to the sharelatex.com window from which the javascript/userscript was launched?
If indeed location.pathname points to the most recent tab rather than the original sharelatex.com window, then how do I get the code to point back to the original ShareLatex window so that it can listen for future button-pushing events there?
I am so confused... Any assistance would be greatly appreciated!

how to pop up a Notice Span when user just log in (but not just refresh to the home page)?

I do not know how to detect it is the first time that user login the web.
I thought i should write a pop-up span on the jsp that user firstly saw when he login.but the issue is then he refresh the page,the notic will show again,that is ridiculous.
I need detect if it is first login means to detect if the user JUST LOGIN or NOT REFRESH the page
how and where shall I detect if it is the first time user login ? and then i can make mind if the notice span pop up.
and I think it should use cookies or session,but which one should i use?
Maintain a field in database table that check if it is first login than show a popup and after that change the value of that field so that Popup do not appear next time.
Ex:
if($data['first_login'] == 1)
{
// show popup
}
If you want to show it only to the new user (the time user registers) you can use a table column in database where you can use it to check if the user if logging in for the first time (e.g firtsLogin the column name = 1 ). If he is logging in for the first time you show the pop-up and change the value of the field to 0.
Otherwise if you want to show to users that are logged in to a specific device for the first time you should use cookies.
I suppose that you want to detect the user logging in to your web-site the first time. There are multiple ways that you can do it depending on your desire to spend additional time writing the code, the location of your logging-in logic (client or server side), security that you want to have while proving your users with login functionality. In all cases - you would have to store the data whether the user has logged in for the first time. I think you are seeking a fast solution that will work without a big care for privacy or security as working with client-side cookies isn't the safest way to store data. The alternatives to cookies are web tokens, url query string, server-side sessions and data-base (RDBMS) storage.
Storing and retrieving the data on the client-side using COOKIES. They are the pieces stored in the user's web browser. Cookies were created to help servers remember the data about the user next time he enters the web-site. The most common usages are: to store location (if accepted by user), web-site locale (language in which the user was browsing the site), products added to cart, etc. Following that approach you can create cookie after the user has logged in to your web-site as follows:
This should be run by your JavaScript.
document.cookie = "firstLogin=true";
After having done that, you would have to add JavaScript logic that will hook-up to user's/client's COOKIE data and read up whether he has logged in the first time before.
This would probably look like a simple JavaScript cookie look-up.
var cookieData = document.cookie;
This will return all of your user's cookies that has been previously stored when he visited your web-site. It will return it as a string concatenated with "; ". If we had previously stored only one cookie, we would get firstLogin=true;
In case if you have multiple cookies set before, you would have to parse the cookie string and extract the data either imperatively by plain procedural JavaScript code or by writing the function which will be able to do that repeatedly. Detailed examples of writing such functions could be found here.

Removing query params causing issue on page refresh

I have a URL with a query string attached to it. After the page loads, I am removing the query param using
history.replaceState("","",location.href.substring(0,location.href.indexOf("?")))
But when user hit refresh button its displaying error screen from my application since expected query param was not there in URL. I can use post action there but I would like to avoid query params with page refresh working fine. Is there any solution for this ?
If you're using a query parameter and then removing it, then the user hit's refresh and there is an error saying it's expecting a query parameter, then your code (and logic) is lacking.
My suggestion, if you want to avoid query parameter after the page initially loaded, is to save the parameter in a cookie or even a session if there is code-behind. Then if the user refreshes without the query parameter, check if the cookie exists, if the cookie exists, show them the page normally, if not, instruct them that they did not visit the URL through the correct channels.
However, like I mentioned, I think your logic here is flawed, because if the user wants to share your URL to the outside world, and you removed the key piece of information that makes the page load successfully, then you will have a lot of confused people on your hands
edit
You might want to investigate SEO friendly URL's to pass your query in to, so instead of: yoururl.com/?firstname=joe you can do yoururl.com/firstname/joe

Forward redirect to previous page

I have a webpage which is linked to from a number of different webpages, a user clicks a link on one of the parent pages, arrives at the webpage and completes a task. When the task is complete the user needs to be redirected to the previous page.
I'm aware I could use the window.history.back() / window.history.go(-1) functions - however what i'd really like is something which the browser considers a 'forward' action so as not to confuse the users if they click the forward/back button after a redirect.
I've tried using document.referrer but this gets blanked in the event of a page reload on the webpage.
The solution would need to be compatible back to IE8 (unfortunately!). Any suggestions would be much appreciated.
Fetching referrer URLs can be unreliable no matter which language you use https://stackoverflow.com/determining-referer-in-php
The best option is to pass the parent url to the task along with the data that you are already sending, despite the initial time outlay (updating the parent pages to do this) it would be the most reliable.
You could try and do a similar thing with a session or cookie to store the url upon starting the task, but it depends on how you have written the current task as to wether you would still need to modify the parent pages.
What about providing a parameter (it may be even the whole url to go after the action) that will let the page know where to go? (like login pages usually do).
It will probably imply modifying all your pages, so it may not be a solution in your case.

Javascript Cookies, set after site close or

Ok, this might sound strange but i need to somehow set cookies on site close, or maybe if somoeone knows different logic i could use to solve my problem:
I am using open source solution to track clicks on my site - http://www.labsmedia.com/clickheat/index.html .Ive change this script to add few more filters for every click such as what OS is user using, referrers, and is user new or returning for which I am using cookies - using mostly this script for cookies http://www.ravelrumba.com/blog/firstimpression-js-library-detecting-new-visitors/ .
Now the problem i have is this. First time you come to site and click, you will be marked as new user, but just with next click u are marked as returning user. Because cookie was set at start and with each click, through GET i am sending these parameters , one of them is testing if cookie exists on site, if it does it sends that you are returning visitor. You can see at this debug link - http://gjakovljevic.com/index.html?debugclickheat , at the bottom it asks "New = yes/no".
So I need solution that will mark user as new with whole first visit, not just with first click, so once he leaves site and comes back, this time his clicks will be marked as clicks from returning visitor.
Here is the link to full script -
http://www.putuj.org/clickheat/js/clickheat-original.js
line 167 is cookie function and line 239 is testing if visitor is new or not.
I hope that i explained well. I am playing with this heatmap script, managed to change and add alot of nice features but I stuck with cookies :)
well the best solution for me here is the following
have a database record of the new visitor, from its ip address and all the details you can gather on its browser he or she is using
then set a duration where in the new visitors's record is considered as a returning visitor
if the user returned with the same ip address and details check if the user already came back is already out of the set duration so it means the user is already a returning user, else he is a new user.

Categories

Resources