Apple Developer Connection
Advanced Search
Member Login Log In | Not a Member? Contact ADC

Safari FAQ

Apple has taken the standards-based approach when designing Safari, which means supporting Safari is relatively simple and painless. Most developers are happy to follow the W3C guidelines and have their pages "just work," with no need for browser-specific HTML. Safari has the features you'd expect of a modern browser plus a few unique ones; understanding these, as well as a few Safari development tricks, will ensure your pages work exactly the way you and your users expect. The source code for the scripts below are contained within the <head> element of this page; Use the Dump Scripts Bookmarklet below to see them all.



Safari Developer FAQ

  1. How do I detect the language setting for Safari?
  2. What is the Safari user-agent string?
  3. Does Safari support DHTML?
  4. How do I determine methods for accessing objects?
  5. How do I prevent my pages (and cookies) from being cached in Safari?
  6. How do I test for cookies using JavaScript?
  7. How do I add a Favicon (the small icon in the address bar)?
  8. What version of Java does Safari use for running Applets?
  9. How do I determine if Java is Available?
  10. How does Safari handle server timeouts?
  11. What type of plug-ins does Safari support?
  12. Where should Safari plug-ins reside?
  13. How do I ensure that my pages will work in Safari?
  14. How do I debug JavaScript in Safari?
  15. How do I report a Bug in Safari?
  16. What CSS properties are supported in Safari?
  17. Why are my Pop-Up Windows not appearing?
  18. Does Safari support Live Connect?
  19. Does Safari support XMLHttpRequest objects?
  20. Does Safari support Tool-Tips?
  21. Does Safari support XSLT?
  22. How do I tell Safari I have an RSS feed?
  23. Does Safari work with .NET and ASPX based sites?
  24. Does Safari support Frames, Framesets and iFrames?
  25. What's different about Safari running on an Intel-based Macintosh?


1. How do I detect the language setting for Safari?

Safari, WebKit and Gecko based browsers support the navigator.language property, while Internet Explorer 5 supports navigator.userLanguage. The following function returns a browser's default language in an alert box:

	function showLanguage(){
	  var languageinfo = navigator.language ? navigator.language : navigator.userLanguage; 
	  alert ("And the Language is: " + languageinfo); 
	}

Here it is in action:

Back to Top 

2. What is the Safari user-agent string?

The complete user-agent string for Safari 2 and below running on Mac OS X with a PowerPC or Intel processor:

     Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/XX (KHTML, like Gecko) Safari/YY

     Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/XX (KHTML, like Gecko) Safari/YY

For each, "XX" is the build version of the web technology framework used by Safari and "YY" is the build version of the Safari application.

The complete user-agent string for Safari 3 running on Mac OS X with a PowerPC or Intel processor and Windows:

   Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_3; en-us) AppleWebKit/XX (KHTML, like Gecko) Version/ZZ Safari/YY

   Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_3; en-us) AppleWebKit/XX (KHTML, like Gecko) Version/ZZ Safari/YY

   Mozilla/5.0 (Windows; U; Windows NT 5.1; en-us) AppleWebKit/XX (KHTML, like Gecko) Version/ZZ Safari/YY

Safari 3 or greater on all platforms contains the additional Version/ZZ portion in the user-agent string - where "ZZ" is the Safari family version number. In addition, Safari 3 and greater on the desktop provides the version of the current operating system running the browser.

The complete user-agent string for Safari 3 running on iPhone:

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/XX (KHTML, like Gecko) Version/ZZ Mobile/WW Safari/YY

Safari 3 on iPhone contains the additional Mobile/WW portion in the user-agent string - where "WW" is the operating system version number.

If you need to detect the version of the browser accessing your site, use the AppleWebKit/XX portion of the user-agent string. A given build version of WebKit, the rendering and JavaScript engine embedded in Safari, provides consistent site compatibility in Safari or any third party application embedding WebKit to render page content.

The WebKit project page provides an excellent library you can leverage on your site -including detection of Safari on mobile devices.

For example: AppleWebKit/125.5.5 or AppleWebKit/125.3 or AppleWebKit/125

And remember, since the rendering engine used by Safari behaves most like Netscape, the Safari JavaScript engine will report navigator.appName as "Netscape". Other Navigator values include:

navigator.appCodeName = "Mozilla"navigator.appName = "Netscape"
navigator.appVersion = "5.0 ..."navigator.platform = "MacPPC", "MacIntel", "iPhone", "iPod" or "Win32"
navigator.product = "Gecko"navigator.productSub = "20030107"
navigator.vendor = "Apple Computer, Inc." 

Show your browser's value for the JavaScript object: navigator.

Back to Top 

3. Does Safari support DHTML?

Safari supports DHTML through the W3C DOM-2, which provides access to almost every element on a page via JavaScript. If you'd like to learn more, check out Apple Internet Developer's two-part series on working with the DOM-2: Part I, Part II.

When testing for DHTML support in your scripts, check for the document.getElementById method, which is also supported by Mozilla, Internet Explorer 5+, and other modern browsers:

	if (document.getElementById)
	{
	 // browser supports W3C DOM
	}

Note that older browsers support DHTML through other DOMs. Internet Explorer 4 supported a DOM that used document.all as a container object while Netscape 4 supported a DOM that used document.layers. To test if any of these DOMs are available in a browser, you could use the following:

	if (document.getElementById || document.all || document.layers)
	{
	 //browser can handle DHTML
	}

Here's how it would look:

Back to Top 

4. How do I determine methods for accessing objects?

Object detection is the best way to test a browser's capabilities. The previous example provides a brief example of object detection: the existence of an object (document.getElementById) determines the actions of a script. This Internet Developer article gives additional detail on object detection and shows how the approach is more flexible and robust than browser sniffing.

Back to Top 

5. How do I prevent my pages (and cookies) from being cached in Safari?

Safari achieves much of its performance through efficient use of content caching. If you have pages you wish to prevent from being cached, including those that write out cookies, be sure to write the following headers when serving your pages:

	// PHP example - include at the top of your pages
	<? php
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
		header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache"); // HTTP/1.0
	?>
	// JSP example - call before writing page content
	response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
	response.setHeader("Pragma","no-cache"); //HTTP 1.0
	response.setDateHeader ("Expires", 0); //prevents caching on a proxy server as well

Safari's Back/Forward cache (the cache pulled from when a visitor presses the Back or Forward browser buttons) can also be thwarted by insuring that your page contains a frame. Frame-based pages are never stored in the back / forward cache and you can insure your non-frame based page behaves similarly by adding the invisible iframe below.

	<iframe style="height:0px;width:0px;visibility:hidden" src="about:blank">
		this frame prevents back forward cache
	</iframe>

Back to Top 

6. How do I test for cookies using JavaScript?

Safari ships with a conservative cookie policy which limits cookie writes to only the pages chosen ("navigated to") by the user. This default conservative policy may confuse frame based sites that attempt to write cookies and fail. Be sure to check the Safari preferences before assuming that your cookies are not written due to a bug; it may just be the users preference.

The following functions set a cookie and test for its existence:

	function testCookies() { 
	 var exp = new Date(); 
	 exp.setTime(exp.getTime() + 1800000); 
	 // first write a test cookie 
	 setCookie("cookies", "cookies", exp, false, false, false); 
	 if (document.cookie.indexOf('cookies') != -1) { 
	   alert("Got Cookies!"); 
	 } 
	 else { 
		alert("No Cookies!"); 
	 } 
	 // now delete the test cookie 
	  exp = new Date(); 
	  exp.setTime(exp.getTime() - 1800000); 
	  setCookie("cookies", "cookies", exp, false, false, false); 
	 }
	
	function setCookie(name, value, expires, path, domain, secure) { 
	 var curCookie = name + "=" + escape(value) + 
		((expires) ? "; expires=" + expires.toGMTString() : "") + 
		((path) ? "; path=" + path : "") + 
		((domain) ? "; domain=" + domain : "") + 
		((secure) ? "; secure" : ""); 
	 document.cookie = curCookie; 
	}

Give it a try:

Back to Top 

7. How do I add a Favicon (the small icon in the address bar)?

Putting the following lines within your <head> tag will add a Favicon to your site:

	<link rel="icon" href="favicon.ico" type="image/x-icon" />
	<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />

If you'd like to learn how to create a Favicon, take a look at this tutorial.

Back to Top 

8. What version of Java does Safari use for running Applets?

Safari uses the most current version of Java delivered with Mac OS X. Updates to Java are periodically available via software update or for download from the Java technology page. Review the Identifying Java on Mac OS X Technical Note to learn how to identify the particular version of Java running your applet.

Use the Java Plugin Settings application ( Applications/Utilities/Java/ ) to display the Java Console while running applets in Safari and to select Safari's default JVM.

Back to Top 

9. How do I determine if Java is Available?

Safari supports Java Applets however some users may prefer to disable its use. Use the javaEnabled() function of the navigator variable to determine its availability.

Here it is in action:

Back to Top 

10. How does Safari handle server timeouts?

Safari on pre Mac OS X 10.3.6 systems will time out if it does not receive a response to an HTTP request or if server data transmission stops for 60 seconds. Safari on Mac OS X 10.3.6 and above effectively removes the 60 second limitation by setting the response timeout value to one year. That should be plenty of time for a server-side process to complete!

Back to Top 

11. What type of plug-ins does Safari support?

All versions of Safari support Netscape-style plug-ins. In additon to Netscape-style plug-in support, Safari 1.3 and above on Mac OS X supports Cocoa plug-ins. While not cross platform, Cocoa plug-ins are easy to create and allow your plug-in to leverage all of Cocoa's frameworks.

For more on writing Cocoa plug-ins, review the WebKit Plug-In Programming Topic and try the sample plug-ins available in the WebKit examples directory provided with Xcode.

For more on writing Netscape-style plug-ins for Safari, see Apple's Technical Note on Browser Plug-ins for Mac OS X and Netscape's definitive Plug-in Guide. A very simple Sample plug-in is available, however you will need the appropriate header file "npapi.h" from Netscape to build the project yourself.

With the transition to Intel-based processors, developers should always create universal binaries for plug-ins written with Carbon, Cocoa, or BSD APIs. For information on how to create universal binaries, see Universal Binary Programming Guidelines.

Back to Top 

12. Where should Safari plug-ins reside?

To insure that a given plug-in is available to all users of Safari as well as other WebKit-based applications, browser plug-ins should be placed in the '/Library/Internet Plug-Ins/' directory.

For Safari 3.0 on Windows, browser plug-ins may be placed in the 'C:\Program Files\Safari\plugins\' directory or any other appropriate location provided that the location is placed in the registry under the registry key '\Software\MozillaPlugins'.

Back to Top 

13. How do I ensure that my pages will work in Safari?

Safari renders HTML in conformance with the W3 specification. You can validate your pages for compliance with the W3 specification via the web-based W3 HTML Validator. If you want to run the validator inside your firewall, follow this recipe and validate yourself!

Be sure to have a DTD defined in your page, pass the validation and you too can proudly display:

Valid HTML 4.01!

In addition, Bookmarklets or Favlets are extremely useful tools to keep handy during your page development. Drag the links below to the Safari Bookmarks Bar to save as a Bookmark then use them to help you debug.

JavaScript Related
Dump Scripts - Displays a new window containing all the JavaScript on or linked to the current page. Use to look for unsupported DOM calls like document.all, document.layers, etc...
Execute Arbitrary Scripts - Displays an input dialog that allows you to execute JavaScript in the context of the current page.
Layout Related
Show Tables - Displays a border around all tables, rows and columns. This one is very useful for determining if your tables are well-formed.
Show DIV Borders - Displays a border around all DIV elements on the current page.
Show DIV Borders with their IDs - Displays the ID and a border around all DIV elements on the current page.
Validation
Validate HTML for Current Page - Sends the current URL to the W3 for HTML Validation. URL must be visible outside the firewall.
Validate CSS for Current Page - ends the current URL to the W3 for CSS Validation. URL must be visible outside the firewall.
Validate Links (HREFs) for Current Page - Sends the current URL to the W3 for Link Validation. URL must be visible outside the firewall.

Back to Top 

14. How do I debug JavaScript in Safari?

Safari's "Develop" menu allows you to turn on the viewing of JavaScript errors and more. To display the Develop menu in Safari 3.1 or higher, select the checkbox labeled "Show Develop menu in menu bar" in Safari's Advanced Preferences panel.

Safari 1.3 and above supports explicit logging of arbitrary information - similar to Objective-C NSLog() - function by using window.console.log() in your JavaScript. All messages are routed to the JavaScript Console window and show up nicely in a dark green, to easily differentiate themselves from JavaScript exceptions.

	
	if(window.console) {
		window.console.log("I think therefore I code!");
	} 
	else {
		alert("I think therefore I code!");
	}

Using Safari 1.3 or above? Open the JavaScript Console and click to see it in action!

Back to Top 

15. How do I report a Bug in Safari?

If you have found a bug in Safari, we want to know! Please review the basics on how to log bugs and remember, the most important aspect of your bug report is a concise reproducible example that demonstrates the problem. Bugs that can reproduced independent of your site or product are infinitely easier to fix.

Looking to certify your web-based application or online service with Safari and don't know where to start? Sign up as a developer and send us a note.

Back to Top 

16. What CSS properties are supported in Safari?

Safari is designed to provide the most complete implementation of the CSS specification. There are however, some CSS properties that are not available or only partially implemented. If your site is utilizing CSS that doesn't appear to work in Safari, please review this list as a sanity check.

Back to Top 

17. Why are my Pop-Up Windows not appearing?

Safari has implemented a pop-up window blocker in order to minimize the distracting pop-up windows many sites use for advertising. The pop-up blocker will filter all pop-ups, except those triggered by a user action, such as a mouse click or a key press. Pop-ups triggered directly from <script> tags, from the onLoad handler, from the onUnload handler, or from timers are blocked.

Be sure to select the "Block Pop-Up Windows" option during QA to be sure that your users don't lose functionality when this very popular feature is used.

Back to Top 

18. Does Safari support Live Connect?

Yes! - Safari 1.2's implementation supports the most popular requirement of Live Connect - communication between Java Applets and JavaScript on the same page. See it in action or download the sample and create your own.

Safari's implementation supports communication between Cocoa and Netscape style plug-ins and Javascript in Safari version 1.3 or higher.

Back to Top 

19. Does Safari support XMLHttpRequest objects?

Yes again! - Safari 1.2's implementation follows the Mozilla model and successfully runs the Mozilla examples at Mozilla.org. Content fetched into an xmlhttprequest object is often XML, however it may contain any kind of textual data and is referenced via the same DOM calls as the document content on a visible web page.

Read this article to learn how to keep web pages connected to XML data on a server using the XMLHttpRequest object.

Back to Top 

20. Does Safari support Tool-Tips?

Safari supports the display of floating text above elements on the page when the 'title' attribute is set. Some browsers may display this "tool-tip" when the 'alt' attribute is populated however, the 'alt' attribute has been created to provide information to screen reading applications for the visually impaired and be used only for such purposes. Move your mouse over the image below to see it in action.

This space reserved for Screen Reading applications only!

Back to Top 

21. Does Safari support XSLT?

Safari 1.3 and above supports XSL transformations of XML pages at load time. XSLT support is limited to XSL page processing instructions embedded at the top of an XML page:

	<?xml version="1.0" ?>
	<?xml-stylesheet type="text/xsl" href="your_transform_file_here.xsl" ?>

XSLT is not accessible via JavaScript XSLTProcessor for arbitrary XML transformations in an HTML page.

Back to Top 

22. How do I tell Safari I have an RSS feed?

Safari 2.0 and above indicates to a user that the page or the site they are on provides an RSS rendition of its content via the link element:

<head>
	<link rel="alternate" type="application/rss+xml" title="RSS" href="/main/rss/hotnews/hotnews.rss">
	<link rel="alternate" type="application/rss+xml" title="RSS" href="/main/rss/hotnews/pr.rss">
</head>

Safari's default preferences are set to poll bookmarked RSS feeds every 30 minutes using conditional GET requests allowing Safari RSS to only pull data from servers that indicate a feed has been modified since the last check. Safari's default preferences are set to cache feeds for a period of two weeks.

Back to Top 

23. Does Safari work with .NET and ASPX based sites?

Naturally! Safari supports any web server architecture capable of deploying pages in a multi-vendor browser environment. Be sure to avoid non-standard features in your development environment that lure you into producing a product that is browser specific.

Improper .NET / ASPX server configuration can account for incompatibilities; For example, the machine.config file contains a <browserCaps> section for sniffing the browser which may not be configured to support any non-IE browser. As a result, developers have reported improperly sized or positioned multi-line TextAreas (<asp:TextBox></asp:TextBox>). ASP.net developers should check out this updated <browserCaps> that supports Mozilla/Firefox, Netscape, Opera, Safari and Konqueror.

Back to Top 

24. Does Safari support Frames, Framesets and iFrames?

Yes, every version of Safari supports the <frame>, <frameset> and <iFrame> elements.

Back to Top 

25. What's different about Safari running on an Intel-based Macintosh?

From the typical web page developers perspective - nothing. Web sites and web applications built with HTML, CSS, and Java Script just work.

If your pages rely on browser plug-ins, be sure that the your plug-ins are built to run on any Macintosh. Browser plug-ins are platform specific and therefore need to be distributed as a Universal Binary in order to run on any Macintosh - regardless of the processor.

Back to Top 


Updated: 2007-06-17