Выполнение JavaScript в локальном документе в Internet Explorer

04.03.2011 19:38 / Артём Волк / 1407 просмотров / ...

Для одного из проектов понадобилось генерировать локальный html-файл с небольшим кодом на JavaScript внутри. По умолчанию при открытии такого документа с локального диска в Internet Explorer 6/7/8 появится предупреждение системы безопасности «В целях безопасности Internet Explorer не разрешает этому веб-узлу выполнение скриптов или элементов управления ActiveX, которые могут получить доступ к компьютеру» и код выполнен не будет:

<!DOCTYPE html>
<html>
	<head>
		<title>IE JavaScript test</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	</head>
	<body>
		<script type="text/javascript">
			alert('Hello, world');
		</script>
	</body>
</html>

Для того, чтобы Internet Explorer запустил скрипт, нужно использовать т.н. Mark of the Web:

The Mark of the Web (MOTW) is a feature of Windows Internet Explorer that enhances security by enabling Internet Explorer to force Web pages to run in the security zone of the location the page was saved from—as long as that security zone is more restrictive than the Local Machine zone—instead of the Local Machine zone. The role of the MOTW is more prominent with Microsoft Internet Explorer 6 for Windows XP Service Pack 2 (SP2) because of increased security restrictions in the Local Machine zone. When you are developing Web content, the MOTW enables you to test your active HTML documents in the security zone where you intend the pages to run. Adding the MOTW to your Web pages also enables you to fully test their compatibility with users' security settings.

Такой вариант будет работать в IE:

<!DOCTYPE html>
<!-- saved from url=(0014)about:internet -->
<html>
	<head>
		<title>IE JavaScript test</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	</head>
	<body>
		<script type="text/javascript">
			alert('Hello, world!');
		</script>
	</body>
</html>