Detect if chrome is loading a cached page? - javascript

This is one of those, "I need a workaround so as not to have to endure 1000 years of bickering from users, once the project launches" type questions:
I have a situation where I need to have my site reload any time someone launches Chrome without closing the tab last time they closed the browser. We are rebuilding an ancient site in a modern, MEAN stack environment, and I just know I will get complaints on this when it launches.
In other words (danger: psuedocode) -
if client closed chrome with site open
reauthenticate user
redirect to home page
I can accomplish the last two bits through an express route and passport auth, but how do I detect if the client is loading a cached page from their end?

Is it possible to simply store some sort of js variable that is only
set true after an actual login, or does Chrome keep that stored on
close as well?
A simple example to go along with my comment:
<html>
<head>
<script>
function DoMagic() {
document.getElementById( "magic" ).innerHTML = "<h1>Hello World!</h1>";
}
</script>
</head>
<body>
<div id="magic">Waiting for magic</div>
<br>
<input type="button" value="Do Magic" onclick="DoMagic();" />
</body>
</html>
Open this in Chrome the div reads "Waiting for magic".
Click "Do Magic" div reads "Hello World".
Close Chrome by clicking X ( not the page, the browser ).
Open Chrome. Page loads. Div reads "Waiting for magic."
The state isn't kept between browser closes--even if the cache is. I'm thinking you can implement something similar to make sure they have to login between browser closes. Should work in all browsers too.

Related

Dismiss an Alert shown by another application

I have a question that may seem slightly vague, but I am not sure how to go about solving it.
Our office runs a headless machine that is a print server, connect via third party software. The issue that I am having, is that if for whatever reason, the system becomes disconnected from the host server, by means of connection issues on either our end, or theirs, an Alert dialog appears, and the printing will be halted until the dialog is dismissed.
The application is run through Internet Explorer, and appears with with the following dialog.
I originally tries using Java, and a Robot to blindly click the okay button, however the issue is that the dialog will appear in different places each time, and sometimes the message will appear multiple times if not dismissed immediately, so blindly clicking a screen coordinate will not work.
My next thought was to search for a PID, and kill each message by name, or PID, however because it is an Internet Explorer application, that is not a possibility.
Any thoughts as to how I could blindly dismiss these messages, either automatically, or through an external signal?
Edit
I have determined that the dialog is created using JavaScript, and I now have an alteration to the question, how can I dismiss this alert() call? I have read that using JavaScript, you can not dismiss the message, but would there be a way to inject a custom script, to overrride the call to alert?
i.e., a dead function that overrides the system alert?
function alert(){
// do nothing
}
How could I append this to the already loaded page...?
I suppose you don't have access to the source code of the print server app so the best way for you to do is add a bookmarklet which contains a javascript code overriding the alert.
A bookmarklet basically is a piece of code stored as a bookmark in a web browser
To create a bookmarklet, open up Notepad and create an HTML page with this content:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<a id="codeOut" href="javascript:(function(){ window.alert = function(){};})();">Your Bookmarklet</a>
</body>
</html>
Save the file as "Bookmarklet.html"
Open the file with Internet Explorer
Right-click on the "Your Bookmarklet" hyperlink, select "Add to favorites" and give it a name
Once your application is loaded and your bookmarklet added, in IE select Favorites->"Name of your bookmarklet"

Switching between the same two tabs only in a browser using javascript and html

My idea of what am trying to do is
When I open a website on one tab of an internet explorer broweser and click on a link it should open a new tab in the same browser with a pdf page init ... the pdf page has a lot of links which if u try clicking on any of them they should take you back to the old tab where u managed to lunch open the pdf from and keep switching between those two tabs only
I hope the idea is clear .. am trying to do this using html and javascript ... this what I wrote but am still missing a lot here. Thanks in advance for any help provided
this piece here lunches another instant in another window
<html>
<head>
<script>
function son()
{
myWindow=window.open('','','width=500,height=500');
myWindow.document.write(" SON !");
myWindow.focus();
myWindow.opener.document.write("<p> DAD !</p>");
}
</script>
</head>
<body>
<input type="button" value="Open " onclick="son()" />
</body>
</html>
This file is where I have the pdf file built in.
<object data="Test.pdf" type="application/pdf" width="100%" height="100%">
<p>It appears you don't have a PDF plugin for this browser.
you can <a href="Test.pdf">click here to
download the PDF file.</a></p>
</object>
thanks again
In the old days, you could use a window's focus method to bring a window/tab into the foreground. However, abuse (mostly in the form of advertisers' popup windows) has resulted in browsers restricting or disabling that functionality.
If we ignore the PDF part, conceptually, this is a very simple request. First, open a window and hold on to its reference:
var pop = window.open('page.html'); // opens in new tab on most browsers
In the secondary page, switching back to the original was simple:
window.opener.focus(); // no longer works in most modern browsers
And from the first page, to switch back:
pop.focus(); // might work
In my test, I couldn't get IE 9 or Chrome 21 to switch back to the opener tab. In Chrome, I could open the second page, manually switch back to the original tab, and calling pop.focus() did bring the second tab back in focus. (IE did nothing.) I was able to force Chrome back to the opening page by calling window.opener.alert('...'); from the secondary page, but that's an ugly hack.
So it looks like you won't be able to pull this off, at least not reliably.
I'm not sure exactly what you're trying to accomplish (a TOC?), but have you thought about opening two windows? Open one with your links that covers the left-hand side of the screen, and another on the other half with the PDF.
JavaScript does not have APIs for controlling tabs. Therefore, you can't do it.
You can open windows, but you can't control if it will be a tab or window.
One alternative possibility involves NOT opening a second window or tab.
If you open another/replacing page in the current window or tab,
you can use the History object to switch between the two pages.
history.go(-1); //takes you back to previous page
history.go(1); //takes you forward to the page from which you went back.

java script not working in chrome

I am working on a website in which I want to close the browser window/tab when the Logout Successful page loads. I am doing this because after the Logout workflow if 'back' button is pressed on browser, it displays the previous page with all the confidential info visible. Since I cleared the session in the corresponding servlet, user cannot do anything after the logout but display of data is of quite concern.
For this pupose I am using below code (Logout.jsp)-
<body onload="self.close();">
<center>
You have been successfully logged out.<br>
</center>
</body>
The problem is that the above code is working fine on IE however, it is not working on Firefox and Chrome. I checked all the settings related to Java Script for Chrome and they are exactly the same as of my browser.
Alternatively, I tried to clear the history. For this purpose I used below code which I found on some website -
<body onload="window.history.forward(0)">
<center>
You have been successfully logged out.<br>
Kindly close the browser for security reasons.<br>
</center>
</body>
But this code is not working in any of the browser.
Any kind of help would be appreciated.
---------------- UPDATE ---------------
For the second option (i.e. clearing history), after much google and work, I was able to make it work. The solution is as below -
If a user follows the navigation page1 -> page2. And you want to stop user from page2 to go back to page1 then add below code on page1 -
<body onload="window.history.forward();">
//rest of the body
</body>
I tested it in IE and found it working perfectly.
Now, I am left with the original issue that why script is not working in Chrome.
Do not do that. Just verify if the session exists. If it doesn't ( since it logged off ) you send him automatically to login page. That way your site is hack proof because i can eliminate from firebug or any console the line where you just close my tab, and therefor i can still see the data.

Legally avoiding popup blocking

What is causing some browsers to see my code as unsolicited?
I have a web site devoted to helping people with interactive sessions. It starts with the user clicking [Begin] so this is a consented action. This should (1) open a popup while (2) redirecting the first page to a end page as below :
<head>
<SCRIPT language="JavaScript">
function openwindow(){window.open("{INTERACTION}","interaction","resizable=0,width=800,height=600,status=0");}</SCRIPT>
</head>
<body>
<FORM action="end.php" method="{METHOD}" >
<input type="submit" class="button"
onClick="javascript: openwindow()"
value="Begin" />
</FORM>
</body>
As said, this is not trying to open an unrequested popup but some strains of IE and Chrome appear to be treating it as such. I have been trying to get a fix, most recently digesting this post.
In it Bobince comments
these days, you don't really need to ask the question “was my unsolicited popup blocked?”, because the answer is invariably “yes” — all the major browsers have the popup blocker turned on by default. Best approach is only ever to window.open() in response to a direct click, which is almost always allowed.I'm quite happy to buy into this principle because I simply want my popup to open.
What is causing some browsers to see my code as unsolicited?
I'd appreciate any help you could give me. (as you might have guessed, client side is not my bag and this topic has been bugging me for ages).
Many thanks in advance (and fingers crossed)
Giles
No much you can do. You could ask your users to disable pop-up blockers or inform them that a pop-up blocker is enabled by checking the window object ref returned by window.open()
e.g.
var w = window.open('http://domain.com');
if(!w) {
//an alert in this example
alert('oops..seems like a pop-up blocker is enabled. Please disable');
}
you could find another way and try what Brad suggests.
There isn't anything you can do about this. Some popup blockers still block everything, even in response to a user clicking. The best you can do is suggest your users turn off popup blockers, or find a different way to do what you want to do. A popular method is the div that appears on top of all others on your page, like Lightbox.
There are many jQuery plugins which make this easy.
You have (at least?) 2 options to deal with this:
if you want to keep using popups, display a very visible warning for your users, pointing them to instructions on how to configure their browser to whitelist your domain (like the banners that appear on top of StackOverlow.com when you gain new privileges, or even like the banners Chrome is showing for actions - they are web-based as well);
use an iFrame and load its content based on your user's click.

Detect when a user leaves a website

I am trying to create my own website access library (for fun) like Google Analytics where I can detect when a user accesses my website, what pages they view etc.
Is there a way to determine when the user leaves a page &/or leaves the website for good?
I have successfully coded (in python) the detecting when the user 1st accesses my site (using a cookie) & how to determine what pages they view. But I don't know how I could detect when they user leaves the website for good?
Is there a way in javascript (maybe I can detect when the page/url is changing?). I know in HTTP there is a referrer header that tells me where the user came from, maybe when the user moves to another website (outside of mine), I can be notified of this (because I will be the referrer in that HTTP request)? Am I correct?
Using jquery you can trigger this:
$(window).bind('beforeunload', function() {
// ajax call perhaps
// triggering a write to db or filesystem...
});
Pure javascript way:
<html>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.somewhere.com">Click here to navigate to
www.somewhere.com</a>
</body>
</html>
As long the user plays by the rules you expect the onbeforeunload will work. That means, closing a tab, or closing the window, or navigating to another site.
However, you have no way to detect this reliably with javascript, onbeforeunload doens't fire in many cases, such as shutting down the browser (ctrl+q), browser crash, history (back) and opera and some versions of chrome have limited support to onbeforeunload.
If you want to detect it with high precision, you must send Ajax requests periodically that shows the user is "still alive". register those requests in a database or file and analyze it by the time sequence.
So, if you "ping" the database every 20 seconds you can know from pretty simple queries that the browser hasn't "pinged" after a short while, and determine the user is no longer in the site.
You can mark all links on your site as inner or outer links. They must point to your site, but then redirect to location, selected by user. Before redirection you can point that user left away from your site.
But.
I'd better putted on every page on your site a little script which (say every 20-30 sec) make a GET request to specific url on your site. So you can track number of each user requests.
There is an unload event you can handle in JavaScript. For example:
window.onunload = unloadPage;
function unloadPage()
{
alert("unload event detected!");
}
Unfortunately, there is no way to tell where the user is actually going when they leave the current page (unlike a referrer, when you enter the page).
One idea is, to set a variable (perhaps in database) in the unload handler (via AJAX call or what not), and then remove it if user enters another page shortly after that. Whichever record is not removed (or deactivated - soft deletes) is your last exit event before the user actually bounced off your web site or closed the browser.
You can bind to the window.beforeunload or window.unload.
Neither of these methods are very reliable though.

Categories