1

我想知道将一些 javascript adsense 代码添加到 200 个或更多用作图片库的现有 html 文件中的最简单方法。

我已经阅读了一些解决方案,例如将 javascript 放入 php 文件,然后插入指向 php 文件的链接,但到目前为止,除非我使用占位符,否则 HTML 不允许这样做?这对我来说并不熟悉。

我正在考虑添加此代码的另一种方式是通过单独的 CSS 样式表,我可以从 index.html 链接到该样式表 .... 有什么想法吗?

4

2 回答 2

0

可以使用 php 将所有文件读取为字符串,找到结束 body 标记</body>并将其替换为 script 标记,</body>然后重写回文件。

或者在可以读取整个目录并查找/替换的 IDE 中执行相同操作。

开始前保存所有备份

于 2013-11-11T16:58:35.590 回答
0

我知道这不是您提出的问题的答案,但它可以在将来帮助您。

当您为每个图像创建单独的文件时,存在一个大问题。您刚刚发现编辑所有这些文件是多么痛苦。有更好的方法来实现它。更好的方法是使用某种模板。然后您只需要编辑模板,然后重新生成文件。

但是您可以跳过它并让脚本在有人要求时动态创建页面。

您可以在下面找到我为您创建的简单 php 脚本,它应该可以帮助您入门。您只需将所有图像添加到数组 $images 中,它就会动态创建您需要的页面。

<?php
  // Add all images here. Asumes that thumbnails and full size images
  // has the same name and is placed in "thumbnails/" and "images/"
  $images = Array(
    Array('src'=>'hamilton_historical_photos_1.jpg', 'title' => 'Title of photo 1'),
    Array('src'=>'hamilton_historical_photos_2.jpg', 'title' => 'Title of photo 2'),
    Array('src'=>'hamilton_historical_photos_4.jpg', 'title' => 'Title of photo 4')
  );


  // Using a seach-variable of 'image' to show a specific image'
  // Note: Arrays are indexed from 0, but we want to be friendly and show
  // the first image when image=1. Therefore 1 must be subtracted to get the
  // real index.
  $idx = @$_GET['image'] -1;

  // Get information about the image. If the index is wrong we don't get
  // any information. That is used later in the code when testing if
  // $image has a value or not.
  $image = @$images[ $idx ];

  // Default title
  $title = "Township of Hamilton Historical photos";

  // If an image was found, add that to the title.
  if ($image) $title .= " - " . $image['title'];


?>
<!doctype html>
<html>
<head>
  <title><?php echo $title; ?></title>
  <link rel="stylesheet" href="style.css" type="text/css">
  <script type="text/javascript">
    google_ad_client = "ca-pub-2656154241161799";
    /* 160x600, created 1/31/09 */
    google_ad_slot = "6877855916";
    google_ad_width = 160;
    google_ad_height = 600;
  </script>
  <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
  <style type="text/css">
    .thumbnails {
      text-align:center;
    }
    .thumbnails img {
      vertical-align:middle;
    }
  </style>
</head>
<body>
<?php
  // Get number of images
  $max = count($images);

  // Test if an image was seleted, if so show that image.
  if ($image) {
    echo "<div class='textbg'>$title</div>";

    echo "<div class='textreg'>";

    $sep = " | ";

    // 'First' should be a link if we aren't on the first image
    $label = "First";
    if ($idx == 0) echo $label; else echo "<a href='?image=1'>$label</a>";
    echo $sep;

    // 'Previous Picture' should be a link if we aren't on the first image
    $label = "Previous Picture";
    if ($idx == 0) echo $label; else echo "<a href='?image=" . $idx . "'>$label</a>";
    echo $sep;

    // 'Next Picture' should be a link if we aren't on the last image
    $label = "Next Picture";
    if (($idx+1) == $max) echo $label; else echo "<a href='?image=" . ($idx +2) . "'>$label</a>";
    echo $sep;

    // 'Last' should be a link if we aren't on the last image
    $label = "Last";
    if (($idx+1) == $max) echo $label; else echo "<a href='?image=" . $max . "'>$label</a>";
    echo $sep;

    echo "<a href='?'>Thumbnails</a>";

    echo "</div>";

    echo "<hr size='1'>";

    echo "<a href='?'>" .
      "<img " .
        "src='images/" . htmlspecialchars($image['src']) . "' " .
        "title='" . htmlspecialchars($image['title']) . "'" .
      ">" .
      "</a>";

  } else {
    // Showing thumbnails
    echo "<div class='textbg'>Twp of Hamilton Historical Pictures</div>";
    echo "<span class='auto-style2'><a href='http://www.hamiltonhistorical.com'>Hamilton Historical Website</a></span>";
    echo "<span class='textbg'> ($max images) </span>";
    echo "<hr size='1'>";


    echo "<div class='thumbnails'>";
    echo "<div class='textreg'>Click a picture to see a larger view.</div>";
    foreach ($images as $idx => $image) {
      $idx = $idx + 1;
      echo "<a href='?image=$idx'>" .
        "<img " .
          "src='thumbnails/" . htmlspecialchars($image['src']) . "' " .
          "title='" . htmlspecialchars($image['title']) . "'" .
        ">" .
        "</a>";
    }
    echo "</div>";
  }
?>
</body>
</html>
于 2013-11-11T19:09:25.433 回答