I would like to create a webpage with browser specific in javascript.
For example:
can I use code like this in my coding part
chrome.tab.onRemoved.addListener() in my webpage.
If it is possible please suggest me.
What exactly are you tring to acheive?
If you want to know when the tab in which your webpage is loaded is closed, then window.onunload should help you.
If you want to know when another webpage is closed, you cannot do this.
UPDATE:
You said that you want to know when the user closes the browser or tab. This is not possible.
But for your purpose (getting feedback), I think all you need is to differentiate whether the user is navigating to a link in your page, or whether the user is typing another URL(or by clicking a favorite).
I think for your requirement, whether the user closes the browser, or whether he types another URL, is the same - the user is navigating away from your site, and at that time you say you want to collect feedback.
This can be done in javascript.
For all the clicks in your page that
might lead to a page refresh
(hyperlinks, buttons,...), set a flag.
In window.onunload, check whether
this flag is set.
- If it is set, then
the user has clicked a link in your
page, do nothing.
- If the flag is not
set then the user is navigating away,
time to collect feedback.
Let me know if this would work.
PS: Note that popups/any distractions during window.unload can be very annoying.
I understand that this probably is the requirements given to you. But if possible, try other mechanisms to collect (voluntary) feedback from the user.
No, you cannot access extension-specific APIs from webpages.
The Navigator object contains all information about the visitor's browser.
https://developer.mozilla.org/en-US/docs/Web/API/Window.navigator
I think this is pretty much the extent of what is possible in terms of interacting with the specific browser. You can't access other tabs (for security reasons) or tell when a tab is closed.
You can use use the onbeforeunload event:
<html>
<head>
<script>
var exit = 1;
function handleClose()
{
if (exit)
{
alert("Closing");
}
}
</script>
</head>
<body onbeforeunload="handleClose()">
Navigate to other page
</body>
</html>
Related
the requeirment is that I want to avoid the specific web page to save to bookmark,
and is there someway to acheive this funcion just use some code, maybe add or js code . thanks
The answer is no, the user can always bookmark a page as this is browser function, but you can use sessions. Then make sure that any request for a page
must have an active session id or it returns an error or redirects to the home page. The user can bookmark the page but the bookmarks will then only work for a short time (until the session expires). This also has the added benefit of
making the site impossible to index by search engines.
The closest you're going to get is if you open another window using JavaScript as you can control whether the menubar and toolbar are displayed.
window.open(
"https://www.google.com/",
"Google",
"resizable,scrollbars,status");
However, this is likely going to be blocked by their popup blocker.
I am currently trying to figure out a strategy for not allowing a user to refresh/reload a page during the quiz. I know it's not a good idea, or even possible, to completely disable the reload feature in the browser, but are there any other ways to keep the user from being able to restart the exam by reloading the page?
I currently have the browser window not displaying the back button, and typing in the url is not allowed. I know I can disable the context menu from the right-click on the mouse, but keyboard shortcuts for reloading the page are still allowed. Any ideas on how I should approach this?
EDIT
I should change this to mean that I am not actually attempting to disable the browser reload feature, but instead, if the user does attempt to reload, I want to redirect them so that the test ends, or some other event takes place, instead of the test restarting.
Currently, I am using a localStorage variable to indicate if it's a reload and going from there. Is this the best solution?
Thanks!
You are fighting an uphill, unwinnable battle if you are attempting to secure a web application using only client-side technology (javascript). There are always ways to get around the measures you're attempting.
A workable solution would be track the quiz progress server-side after every user decision. This way, if the user tries to reload (or otherwise start over, re-answer, or skip around) you can simply return them to where they belong.
I can think of a solution that detects if the page is refreshed.
If you're using PHP, you can set a SESSION variable on loading and then check to see if that variable is set yet
<?php
if(!isset$_SESSION['loaded'])
{
$_SESSION['loaded'] = 1;
}
else
{
echo 'You refreshed, your quiz is over ;-) ';
}
?>
As for disabling refreshing, you'd be bordering on malware to achieve it, ie. filtering all keyboard input for 'F5' key or 'Ctrl + R' from JavaScript, and disregarding keyboard input when they enter those shortcuts. I'm not sure if that's possible/ethical
I would like to identify browser tabs (on my domain) using JavaScript.
I mean that if user open several tabs with my website and submit web form only on one page I want to notify only this page, even if user moves from this page.
It should be max cross browsers solution.
P.S. One of the possible solutions is using "window.name" property, but I do not want to use it because somebody else can use it.
P.S-2: I found one more possible solution: using sessionStorage. It supported by FF3.5+, Chrome4+, Safari4+, Opera10.5+, and IE8+. Oooohhh, I need IE7!!!!
Thank you in advance!
I don't think this can be done. Each browser tab that is opened is basically like a new browser instance. Just like if the user opened another browser. One tab knows nothing about the other tab by design. This is how it should be. Can you imagine the implications if a web site developer could add code to their page to "see" what other sites you have opened in your browser?
window.name is the only persistent data element you can use for this purpose, as described your requirements.
I want to notify only this page, even if user moves from this page.
This is impossible. Once a user navigates away from a page, you lose control over that tab. You can't push to a page, it needs to make a server request FROM that page, even if it's ajax.
Using sessionStorage. It supported by FF3.5+, Chrome4+, Safari4+, Opera10.5+, and IE8+.
For IE7 using "window.name" property.
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.
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.