I implemented a timer in javascript, but what if javascript is disabled? - java

I have used a timer in my webpage implemented in JavaScript. but what if JavaScript is disabled? Will it remove the time constraint from the webpage? If that may be the case what should I do?

It depends on what you are trying to do, but you can use a meta refresh tag to redirect the user to another page (or the same page) after a given time interval.
This will potentially be horribly disruptive to the user experience.

Related

Javascript How to stop or reset a timer in Java on a website

I'm trying to stop a timer from running on a website. I either want to stop it or control its value. The timer id is:
id="timer"
I want to be able to stop it with a bookmarklet, so the code should begin with:
javascript:
I'm new to java, so I don't know the approach to stop a timer.
You are referring to Javascript, not Java. Javascript is incorporated into webpages and doesn't need to be installed. Java does need to install and is used for making applets. Two different things.
As for id="timer", you can set it's value using something like this.
javascript:document.getElementById("timer").innerHTML="Enter new value here";
But be aware that if it is counting down, there is probably a setInterval or setTimeout somewhere in the Javascript code of that page triggering it, and so the above bookmarklet will do nothing and can't do anything based on the information you've provided. One thing you could try is stopping all setTimeouts or setIntervals on the page, THEN calling the code above, but they may break something else in the page.
javascript:window.setTimeout = function(func, delay) {};window.setInterval = function(func, delay) {};document.getElementById("timer").innerHTML="Enter new value here";
To make them bookmarklets, just set those to the href attribute of a link, or type them in as URL of a bookmark.

How to dectect modification on page? Which Listener in java?

Is there any Listener in Java, which can detect, if the content of the page has been changed? "changed" means text in the page has been added/removed...
Process: Author modifys the page and activate it. In publish Instance it must be checked if the page content has been modified/changed
I don't think there is such listener. You're gonna have to reload/access the page or you can hook it up so when the author submits his changes you insert a value to the database that this specific page has been modified. After that you just read the data from the DB using a timer that triggers every now and then and if new line appears you do your action.
This is more of a design question and you should think about what project you're working on and what's the best approach to implement this feature.
Apache sling can handle events. There is nice tutorial here http://sling.apache.org/documentation/tutorials-how-tos/how-to-manage-events-in-sling.html .
Basically create a listener ad check if the event relates to a page node (or its subnode). Then apply whatever logic you want.
Be careful to check whether you are in an author or publish instance ( or turn off the service in author)

How to handle continous page refresh in jsp , java and js code

If user refresh the page continuously using F5 functional key then the page loading is very slow and can be seen blank page for long time.
How to solve this problem?
I tried using cache on server side but I don't think that I am using it in proper way.
Can somebody help me with an example.
I think you need to use browser cache, which can be controlled by http headers, or meta tags.
http://www.mnot.net/cache_docs/
You need to set page cache to be around 5 seconds or some similar value so that no new request will be sent to server in that time interval.
A few things:
You could try to minimize processing time within your application, maybe by minimizing wasteful operations. Sounds like your application spends a lot of time recreating the output.
You could try to add some sort of caching on the server side, and and send the user the same page (ie no "new" processing) for some time. Depending on the mechanism, this may not be feasible though (https, security?). At least, afaik.
Of course you could change the way the site works. You could use Ajax to push information to the site the user is on, and so take the urge to refresh away from him.
And maybe your server just does not have enough power to serve a lot of users at the same time?
It is very difficult to stop user from pressing F5.
Try making your code more optimized.
Use meta tags for cache like:
cache-control
EXPIRES
PRAGMA NO-CACHE
Also check this for JSP caching.
response.setIntHeader("Refresh",5);
just use this jsp method for autorefreeshing of ur webpage...
http://www.tutorialspoint.com/jsp/jsp_auto_refresh.htm

how to bookmark a dynamically created aspx page?

There is this site wich in the address bar only shows like "http://example.com/examplepage.aspx".
Normally if it would have parameters behind it you probably could just copy that one.
But since it doesn't, how do i bookmark this page.
It doesn't necessarily have to be a bookmark, but at least an easy way to access the page.
(fyi I know basic HTML and Java, maybe it's only possible programmatically).
thnx
Generally dynamic pages (taking in context with the question) are not book mark friendly.
You could probably sniff the incoming request, and create a fake form which you can then submit later.
However there may be situations where there are parameters such as session id which are valid for only small periods of time.
You should read up on sessions. In really simple terms, a session is assigned to users accessing a website. They have an expiry period. IF you stay idle beyond set time (determined by the developer) you will not be able to get in. And every time you log back in, you may be assign a new session.
You would have noticed, that some websites automatically log you in, this is mostly done with the help of cookies. Cookies work in tandem with sessions, they store very basic information, so the next time you come back to a website, it will be able to identify you as a returning user and provide you with access.
Then again, some pages don't use sessions, they might have their own custom way of identifying users.
Bookmarks can be used in dynamic pages, if the code allows you to send GET requests, if they don't have any other extra parameters which will block you.
To Summarize:
Dynamic page not very bookmark friendly.
There may be parameters used to access a webpage which change constantly, which you cannot really save.
You may be able to get into dynamic pages using bookmarks, if they don't use any of the dynamically changing parameters.
Since you know Java, you should probably read up on JSPs/servlets to get an understanding of what happens behind the scenes in dynamic pages.
Hope this answers your questions.

Closing popups on session expiry

Here is the thing : my webapp has loads of popups and my boss wants 'em closed on session expiry, coz when session expires and an user presses refresh on a popup, he is being shown the logon page -> user logs on -> user is directed to the dashboard. Now, a dashboard screen in a popup is totally uncool. Here is where google got me:
Have javascript to close popup onload. Generate this onload script into the response if session has expired (checking session expiry from jsp and including onload script conditionally).
Do you think this is a good way to it? What is the best practice for this scenario?
P.S: I am not allowed to use AJAX
In a past life, I made a popup manager object that maintained what windows were open. You should probably make one of these if not already done. Then, you can use setTimeout to call a function after so many minutes (or whatever time you want) have gone by. This will check for recent activity (probably via AJAX) and close the popup if you determine that the session has expired. If not, call setTimeout again with your new time, properly adjusted for most recent activity.
^^before the AJAX edit.
Well, since you can't use AJAX, can you put something in the url that will tell you it's a popup? Then you'll know not to show the login screen when the user hits reload.
The best way would be an XMLHTTP request to check login and close them if required - do this periodically.
Astute readers (meaning everyone) will notice that this is an AJAX request, but if you phrase it that way it might get accepted as whoever dictated that you 'aren't allowed to use AJAX' is clearly an idiot.
An alternative way to implement modal dialogs in a web application is to:
Model the dialog in a DIV, default styled to display: none;
On desired action, inject/append the Modal dialog DIV into the page source
Reset the CSS display so the modal dialog DIV is visible, overlaid on top of the page by setting the CSS z-index property
Make the modal dialog disappear upon either successful execution or the user cancelling out
Because the modal dialog is part of the page source, the dialog will disappear when the session times out. This approach doesn't spawn supporting windows that can be orphaned as the poster is attempting to address. And it fits the requirement of not using AJAX.
You can code these by hand, but I don't really recommend it because of having to support various browser. I suggest looking at the Yahoo User Interface. You can tailor it to suit your needs (IE: only modal dialogs), and it would support AJAX if requirements change down the road.
Beware of spawning modal dialogs from modal dialogs.
If your boss is asking you to achieve this, without using AJAX, then you're in trouble. He should understand that the only connection a browser has to the server (without refreshing the page) is javascript (what he understands to be ajax).
The best way to do this is to setup a script on the pages to ask the server if the user is still logged in every 30 seconds or so.
setInterval(function(){
$.get("loggedin.php", function(result) {
if (!result.isLoggedIn)
window.close();
});
}, 30000);
This script assumes you're using the jQuery framework for rapid development of javascript solutions. This also uses JSON (Javascript Object-notation) to test a return-value from the loggedin.php file.
Bottom line, you need to use AJAX. Tell your boss there is no other way. If he still doesn't get it, ask him to balance his checkbook without using math.
In theory, you could avoid AJAX by using a hidden flash widget...
But more practically, AJAX is the 'right' solution, and I think you will have to talk to your boss, determine where this 'no AJAX' rule came from, and convince him that AJAX is the best way to solve this problem.
Does he think AJAX would be take too much time to implement? If so, you should prove him wrong. Does he think it will be hard to maintain? If so, show how simple the code to do this will be, and how widely used the common AJAX libraries are. If your boss is reasonable, then his goal is to what is best for the product, and you should be able to reason with him.

Categories

Resources