Put your text ad here
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
Top Canadian Hotels no booking fees from Victoria BC to Nova Scotia
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.
meta http-equiv properties will clear the cache!
A: Browsers' so-called user agent string is available through the navigator.appName property, but is often given as a code name or abbreviated name for the browser, not necessarily its common name. For historic reasons most browsers include a reference to the Mozilla code name of early Netscape Web browsers. Microsoft Internet Explorer is usually declared as MSIE. Many browsers reference the Gecko rendering engine used by Firefox. Safari and Google Chrome both mention their WebKit rendering engine. Other browsers often "spoof" their user agent name or at least mention several others. It is best to check on the browser vendors' own Web sites for the code names they issue.
The main reason for the overlap, masquerade and disguise of user agent information is that misguided Web site managers often use this name to deduce the capabilities of Web browsers and attempt to serve content tailored to them. False identification of the user agent name and incorrect deductions can result in browser users receiving an inferior experience on such Web sites, or being shut out altogether, so browser vendors have responded by mimicking each other.
For these reasons you should learn from others' mistakes; do not take user agent strings at face value, think hard before you attempt to manipulate your your visitors' Web experience based on which browser you think they are using and test thoroughly. If you target two or three major browsers, be sure to provide a reasonable experience for the many other browsers and mobile devices out there.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: You can read the application version of the browser that executes your Javascript code from the navigator.appVersion property. This string is part of the user agent identifier that the browser sends to a server when it requests a Web resource and usually includes the browser version number and an indication of the operating system the browser is running on.
This property may be interesting purely for information, but you should be cautions about how you interpret its contents and avoid using it to control script execution. In many browsers, the user agent string can be modified to "spoof" as a different browser type, use arbitrary text or withhold any user agent information at all.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The method to check whether Java is installed and enabled is navigator.javaEnabled(), which returns a boolean value.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: To enable Javascript in Internet Explorer, go to the Tools menu on the browser toolbar and choose Internet Options.... This should open the Internet Options dialogue box. Click on the Security tab, click on the globe icon for the Internet and then the Custom Level button below. Scroll down the list of options to the one headed Scripting. Under the option labelled Active scripting, check the option to Enable. Click OK to back out of each dialogue.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: There is no direct Javascript interface for checking the contents of the browser's cache. It would be a security hazard if you were able to probe the contents of the users' file system. But there are indirect techniques you can use to track pages people have visited on your site, which are likely to be cached.
The key way to record that an HTML document or image has loaded is the onload event that is triggered when the download is complete. These Javascript events can be assigned an event handler function to be called when the event occurs. The event handler function may then store the page information in a cookie.
<body onload="logPage('Example.htm');" >
Though you can be sure that documents and images have downloaded, there is no certainty that they are actually cached, browsers are not obliged to do so and it is possible to disable caching. Still, your Javascript application can make an assumption that these assets are cached provided it does not rely on the fact, and deals with the possibility that they are not.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: No, the Javascript object model includes a window.print() method that may activate the standard print dialogue of a Web browser, but that is as far as the functionality extends. It would not be appropriate or safe for Javascript code to be able to check the printers attached to a computer, look up printer properties or arbitrarily configure their settings.
If you want to influence the way a Web page is printed, you will find it much more effective to use Cascading Style Sheets for print media. See the Media style sheets FAQ for further details.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: There is no DOM standard object reference to system printers and therefore no Javascript or ECMA script application programming interface for selecting amongst available printers. The basic DOM level 0 function for printing is window.print(), which should raise a print dialogue. Whether users have any printer attached to their computer, decide to choose amongst them and to commit the print job is outside the control of Javascript. It would be a security hazard if Web page scripts could arbitrarily control computer peripherals and expend paper and ink without user intervention.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Javascript has practically no print configuration options, so it is not possible to control whether the page background is printed or not. If you want to prevent the page background from being printed you can use a media dependent style sheet to set a plain background colour and override any background images that have been set.
Print media style sheets are attached using stylesheet link elements in the head of an HTML document with a media="print" attribute, as below.
… premium content omitted
Access all premium content for $50: sign-up now.
Can we edit the print background property with Javascript?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: You should not attempt to do this with people's Web browsers, even if it were possible. If you press channel 1 on your TV remote and it mutes the sound, most people would say its broken. If you attempt the equivalent with a Web browser most people will leave your site immediately.
If you want people to save your Web pages, you will need to give them a very good reason to do so and explain how to do this manually. The Web Archive format, .mht is a Microsoft proprietary format that is only supported by Internet Explorer, most other browsers would save a page in its original HTML format.
If you want to prevent people from printing your Web pages, you have set yourself an impossible problem. The only solution is not to publish your content on the Web. There are many ways to get around any restrictions you may put in place.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: It should not be possible to clear a browser cache using Javascript because this would be a security hazard.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: There is no way to switch on Javascript in a Web browser if it is not enabled. It would be a serious security hazard if code in Web pages could override users' browser settings in this way.
People choose to switch off Javascript because some Web developers mis-use Javascript to affect the way their browser works; typically with pop-up windows and redirects to pages they have not chosen to visit. Some Web browsers do not support Javascript at all, so your Web pages should be designed to provide useful information for people without Javascript.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: It is always a bad idea to try to stop people's Web browsers working the way they expect. There are very good reasons for a print dialogue to be shown; so that it can be cancelled in case print was requested by mistake, so that the settings can be adjusted as required for the content, so a different printer can be selected. Can you be sure people actually have a printer connected?
You should re-consider why you want to disable the print dialogue and perhaps provide more guidance to the user about what you would like them to do. The media dependent style sheet FAQ may give you some further ideas.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: It is never a good idea to stop people's browsers working the way they should and you should look again at why you want to stop them doing things that are fundamental to the Web. There are techniques that can make these operations difficult, but they can be overcome simply by switching off Javascript in the Web browser or using a different client.
Any efforts to stop people taking copies of content that is published on a public Web address are futile and likely to impair the usability of your site. If you do not want to risk the possibility that people will take copies of your source code and images, do not publish them on the Web or find a different way to achieve your goals.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: This is another example of something you should not attempt to do with Javascript, it is never a good idea to try to prevent a person's browser working the way it is intended, some would regard it quite hostile. In this case, the objective is completely futile; any HTML, image, Javascript or CSS that is published on the Web is completely in the open and can be saved in numerous ways. In most cases Web resources are immediately saved in the browser cache.
If you are trying to protect a digital resource that is truly valuable, you should not make it available on the Internet.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: It is a fundamental fact that any content made available on the internet using the HTTP protocol is available to download, view and save by anyone who chooses to do so. You can require people to authenticate themselves, but this extra obstacle does not alter the basic fact. Therefore it is a mistake to believe that content on the web can be protected in any meaningful way by disabling a person's Web browser.
Many would regard it as a hostile act if a Javascript were to disable the "Save as" dialogue, "View source" or new window action. Just like pop-ups, these futile gimmicks really annoy people, will rapidly turn them away from your site and give Javascript authors a bad name.
It is a matter of opinion that one should not alter the standard behaviour of Web browsers, but it is a well founded one. This site is read by many Javascript beginners and it is critical they understand this point and avoid bad practice from the outset.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: It is not possible or desirable for Javascript code on a Web page to disable fundamental browser features such as URL resource caching. You should re-think the issue you are facing and consider a different way to tackle it. For example, you can set HTTP headers to request resources are not cached, and that cached resources are refreshed after a specific period, or that the freshness of cached resources is checked before re-loading a page. Web user agents are not obliged to follow these directives, but most will be configured to do so. See the Site manager FAQ for cache configuration examples related to stylesheets.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
meta http-equiv properties will clear the cache!
A: The point of the answer to the original question "Is there a method to clear the browser cache?" is that once Web content is cached there is no Javascript method to clear the cache. It is always best to enable HTML pages, CSS and Javascript itself to be cached if the content is static and appropriate to be stored in the browser and intermediate public caches. This can create a significant performance improvement for Javascript files that are referenced across multiple pages, for example, and reduces the load on the Web server.
There are various methods to request that Web content is not cached in the first place, but this is different from clearing existing cache items. The most effective and reliable way to manage cache properties of content is with HTTP headers, see this Caching Tutorial for an explanation why HTML meta tags are not reliable and alternative examples.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: It is never a good idea to stop people's software from working the way they expect it to. You should re-think why you want to do this. Is there a problem with your application design? Don't try to break your users' Web browsers to work around it, they will not thank you for it.
The Ctrl-N keyboard combination is probably one of the least commonly known ways to open a new Web browser window and there are several more obvious ways to do so anyway.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Server side sessions are a mechanism that has been developed to work around the stateless nature of the HTTP protocol. If your application fails because a user can open a new browser window and store duplicate data then it is a fault in your application design, not the HTTP protocol or the session mechanism. This user behaviour cannot and should not be blocked. If you want to prevent users duplicating content or encountering "illegal" states in your application, you should design your application to check for duplicate content submissions and properly handle inconsistent states.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The original answer still stands. You need to re-design your application to validate data before it is stored in a database, not break standard browser behaviour. Join the Code Style Twitter feed for details of our free one-to-one help sessions.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: A Javascript author can only track the number of open tabs in a browser if the tabs are opened with the window.open() method, but this would not count any other tabs so is incomplete. It would be a privacy hazard and potential security problem if Javascript could read any other tabs open in the browser or generally communicate between Web applications, generally known as cross-site scripting.
If you feel your application needs to know how many tabs are open in the browser it indicates a flaw in the design. You should ask yourself why this information is important. If you have an answer to that question, ask why that reason is necessary, and again until you come down to the fundamental reason, then think again. Is the fundamental reason a sign of a different problem, is it truly necessary, can you think of a different approach or use a different technology?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
About us: site help, text ads and premium content FAQs.