我无法将 JavaScript 代码包含在由 tex4ht 生成的 HTML 标头中。
我想要包含的脚本是:
<script type="text/javascript">
var _gaq = _gaq || [];
var pluginUrl =
'//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
_gaq.push(['_setAccount', 'UA-45193039-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
有人能告诉我怎么做吗?
\JavaScript .....\EndJavaScript
Tex4ht 在线手册(mn)中的命令没有什么帮助,并且给出了大量难以理解的错误消息。
赫尔曼·阿德尔
答案1
在这种情况下,手册没有多大帮助,甚至会产生误导,因为\JavaScript
默认情况下命令没有定义,只有javascript
使用选项时才会定义。但定义自己的版本相当容易:
\Preamble{xhtml}
\begin{document}
\ScriptCommand{\JavaScript}{\HCode{\Hnewline<script type="text/javascript">\Hnewline}}{\HCode{\Hnewline</script>\Hnewline}}
\JavaScript
var _gaq = _gaq || [];
var pluginUrl =
'//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
_gaq.push(['_setAccount', 'UA-45193039-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
\EndJavaScript
\EndPreamble
在这个配置文件中,\JavaScript
首先定义命令:
\ScriptCommand{\JavaScript}{\HCode{\Hnewline<script type="text/javascript">\Hnewline}}{\HCode{\Hnewline</script>\Hnewline}}
命令\Hnewline
用于插入换行符。然后插入 JavaScript 代码而不<script ...>
带标签,因为\JavaScript
命令会处理此问题。
重要的事情之一是将 JavaScript 放在\begin{document}
配置文件之后,这确保它被插入到HTML
标题中:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd-->
<html xmlns="http://www.w3.org/1999/xhtml"
>
<head>
<script type="text/javascript">
var _gaq = _gaq || [];
var pluginUrl =
'//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
_gaq.push(['_setAccount', 'UA-45193039-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<title>mytitle</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)" />
<meta name="originator" content="TeX4ht (http://www.tug.org/tex4ht/)" />
<!-- charset=utf-8,html,xhtml -->
<meta name="src" content="sample.tex" />
<link rel="stylesheet" type="text/css" href="sample.css" />
</head><body
>