Your banner ad here

WestNIC provides reliable web hosting services

25% off cpanel web hosting and reseller hosting deals. Promo: codestyle25off

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs and more.

RSS news feed Get the latest answers in this FAQ

Follow the Code Style Twitter feed for free one-to-one help sessions by instant messenger: MSN, AIM and Yahoo! Messenger. Sessions are held most Saturdays. Join the Twitter feed then check your direct messages for details.

Check browser properties

Q: Can I check whether Java is enabled in the browser?

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.

Q: How do I enable Javascript in Internet Explorer?

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.

Q: How can I get the operating system the browser is using?

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.

Q: Can I check whether a page is cached by the browser?

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 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.

Browser print settings

Q: Can I set the printer properties using Javascript?

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.

Q: Can I select a particular printer using Javascript?

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.

Q: Can we edit the print background property with Javascript?

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: Follow this link for subscription information More details available to premium content service subscribers:
Can we edit the print background property with Javascript?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Controlling the browser

Q: Is there a method to clear the browser cache?

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.

Q: Is there any way to enable Javascript if it is off?

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.

Q: Can I use Javascript to save a Web Archive when the user tries to print the page?

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.

Locking down the browser

Q: How can I suppress the print dialogue?

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 they 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.

Q: How can I suppress Ctrl+N for new window?

A: It is never a good idea to stop people's software from working the way they expect it too. You should re-think why you would want to do this. If there is some shortcoming in 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.

Q: How do you disable view source and save image options?

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.

Q: How do I disable the "Save As" option in IE using Javascript?

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.

Q: Can I use Javascript to disable the browser cache?

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.

Q: Why do you pretend to answer these browser lock-down questions?

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.

Add this page to your chosen social bookmarking service

Style warning - please read

Home · CSS · Java · Javascript · HTML · Help · Log