【问题标题】:Number appearing in Perl-generated JavaScript code instead of "$("Perl 生成的 JavaScript 代码中出现的数字而不是“$(”
【发布时间】:2017-08-19 00:55:23
【问题描述】:

在我正在处理的一个网站上,随机数出现在一个非常简单的 jQuery 中。

这些数字 — 48等 — 出现在开头,而不是应显示的内容。像这样:

<script type="text/javascript">
    48 48document).ready(function () {
    48 48".closed").click(function () {         
        48 48this).find("div.but").toggleClass('plus').toggleClass('minus');           
             48 48this).toggleClass('closed').toggleClass('open');    
        });    
    });        
</script>

我什至无法想象为什么当我将它硬编码到站点中并且没有外部文件或任何东西时它会吐出这个。

好的,既然是 Perl,我需要转义 $(,但是如何?我看了看 Perl 文档,感觉自己被打了一拳。

我是否只使用单引号而不是双引号?

<script type="text/javascript">
    $(document).ready(function () {
      $('.closed').click(function () {          
        $(this).find('div.but').toggleClass('plus').toggleClass('minus');
        $(this).toggleClass('closed').toggleClass('open');    
      });
  });
</script>

这是完整的 Perl 脚本。它是网站的标题。

###########################
## META DATA AND HEADERS ##
###########################

$be_strict = 0;    # 1 = 404 on page not found
if ((!$title{$loadpage} || !$h1{$loadpage} || !$description{$loadpage} || !$keywords{$loadpage} || !$breadcrumb{$loadpage}) && $be_strict) {
  print "Status: 302 Found\r\n" .
      "Location: http://www.realots.co/404/\r\n" .
      "\r\n";
  exit(0);
} 

if ($action) {

  #############################
  ## Highscore page SEO Hack ##
  #############################

  if ($action eq "fish") {
    $breadcrumb{'highscores'}     = "<a href='/'>RealOTS</a> &raquo; <a href='/highscores/'>Highscores</a> &raquo; <b>Fishing Skill</b>";
    $h1{'highscores'}             = "RealOTS Fishing Highscores";
    $title{'highscores'}          = "RealOTS Top Fishing Skill";
    $description{'highscores'}    = "Fishing Skill Highscore list of our RealOTS. Check out these amazing fishermen!";
    $keywords{'highscores'}       = "realots,otserv,tibia,highscore,hiscores,fishing,top fishing";
  }
  elsif ($action eq "ml") {
    $breadcrumb{'highscores'}     = "<a href='/'>RealOTS</a> &raquo; <a href='/highscores/'>Highscores</a> &raquo; <b>Magic Level</b>";
    $h1{'highscores'}             = "RealOTS Magic Highscores";
    $title{'highscores'}          = "RealOTS Top Magic Level";
    $description{'highscores'}    = "Magic Level Highscore list of our RealOTS. Check out these fearsome mages!";
    $keywords{'highscores'}       = "realots,otserv,tibia,highscore,hiscores,magic,top magic";
  }
  elsif ($action eq "sword") {
    $breadcrumb{'highscores'}     = "<a href='/'>RealOTS</a> &raquo; <a href='/highscores/'>Highscores</a> &raquo; <b>Sword Fighting</b>";
    $h1{'highscores'}             = "RealOTS Sword Fighting Highscores";
    $title{'highscores'}          = "RealOTS Top Sword Fighters";
    $description{'highscores'}    = "Sword Fighting Highscore list of our RealOTS. Fear the mighty sword wielder!";
    $keywords{'highscores'}       = "realots,otserv,tibia,highscore,hiscores,sword,top sword";
  }
  elsif ($action eq "axe") {
    $breadcrumb{'highscores'}     = "<a href='/'>RealOTS</a> &raquo; <a href='/highscores/'>Highscores</a> &raquo; <b>Axe Fighting</b>";
    $h1{'highscores'}             = "RealOTS Axe Fighting Highscores";
    $title{'highscores'}          = "RealOTS Top Axe Fighters";
    $description{'highscores'}    = "Axe Fighting Highscore list of our RealOTS. Behold these savage beasts with their amazing axe skills!";
    $keywords{'highscores'}       = "realots,otserv,tibia,highscore,hiscores,axe,top axe,axe fighting";
  }
  elsif ($action eq "club") {
    $breadcrumb{'highscores'}     = "<a href='/'>RealOTS</a> &raquo; <a href='/highscores/'>Highscores</a> &raquo; <b>Club Fighting</b>";
    $h1{'highscores'}             = "RealOTS Club Fighting Highscores";
    $title{'highscores'}          = "RealOTS Top Club Fighters";
    $description{'highscores'}    = "Club Fighting Highscore list of our RealOTS. Caveman-like agility all around!";
    $keywords{'highscores'}       = "realots,otserv,tibia,highscore,hiscores,club,top club,club fighting";
  }
  elsif ($action eq "dist") {
    $breadcrumb{'highscores'}     = "<a href='/'>RealOTS</a> &raquo; <a href='/highscores/'>Highscores</a> &raquo; <b>Distance Fighting</b>";
    $h1{'highscores'}             = "RealOTS Distance Fighting Highscores";
    $title{'highscores'}          = "RealOTS Top Distance Fighters";
    $description{'highscores'}    = "Distance Fighting Highscore list of our RealOTS. Death from afar, raining from the sky!";
    $keywords{'highscores'}       = "realots,otserv,tibia,highscore,hiscores,distance,top distance,distance fighting";
  }
  elsif ($action eq "shield") {
    $breadcrumb{'highscores'}     = "<a href='/'>RealOTS</a> &raquo; <a href='/highscores/'>Highscores</a> &raquo; <b>Shielding</b>";
    $h1{'highscores'}             = "RealOTS Shielding Highscores";
    $title{'highscores'}          = "RealOTS Top Shielding";
    $description{'highscores'}    = "Shielding Highscore list of our RealOTS. These tanks are able to withstand tremendeous blows!";
    $keywords{'highscores'}       = "realots,otserv,tibia,highscore,hiscores,shielding,top shielding";
  }
  elsif ($action eq "fist") {
    $breadcrumb{'highscores'}     = "<a href='/'>RealOTS</a> &raquo; <a href='/highscores/'>Highscores</a> &raquo; <b>Fist Fighting</b>";
    $h1{'highscores'}             = "RealOTS Fist Fighting Highscores";
    $title{'highscores'}          = "RealOTS Top Fist Fighters";
    $description{'highscores'}    = "Fist Fighting Highscore list of our RealOTS. They had special training by sensai Chuck Norris!";
    $keywords{'highscores'}       = "realots,otserv,tibia,highscore,hiscores,fist,top fist,fist fighting";
  }
}

#&print_header();
$web_content .= qq{
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <title>$title{$loadpage}</title>
    <meta name="google-site-verification" content="Z97W94Kp7MlIOgiaMAYcFA4Lp4i5hckXdncRarXkBxk" />
    <meta name="google-site-verification" content="I5HTpL8jLRcdrWk3jD83qIFDBkS4G_rmWHlxEq94jLU" />
    <style type="text/css">
      table.outlined {
        border-width: 1px;
        order-style: solid;
        border-color: #9999CC;
        border-collapse: separate;
      }
      a.suggestions:visited{color:#0000FF;text-decoration:none;}
      a.suggestions:link{color:#0000FF;text-decoration:none;}
      a.suggestions:hover{color:#305997;text-decoration:none;}

.trans {
        filter:alpha(opacity=50);
        -moz-opacity:0.5;
        -khtml-opacity: 0.5;
        opacity: 0.5;
}

    </style>
<!--[if IE 6]>
<![endif]-->
<!--[if IE 6]>
<style type="text/css">
html { overflow-y: hidden; }
body { overflow-y: auto; }
img#bg { position:absolute; z-index:-1; }
#content { position:static; }
</style>
<![endif]-->

};

if ($no_index{$loadpage}) {
  $web_content .= qq{    <meta name="Robots" content="NOINDEX,NOFOLLOW,NOARCHIVE,NOSNIPPET" />};
} else {
  $web_content .= qq{    <meta name="Robots" content="index,follow" />};
}

$web_content .= qq{
    <meta name="keywords" content="$keywords{$loadpage}">
    <meta name="description" content="$description{$loadpage}">
    <meta name="author" content="RealOTS" />
    <meta name="Expires" content="never" />
    <link rel="stylesheet" type="text/css" href="/images/style.css" />
    <script type="text/javascript" src="http://www.mollie.nl/partners/js/96481.js"></script>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


   <script type="text/javascript">
    $(document).ready(function () {
    $(".closed").click(function () {


        $(this).find("div.but").toggleClass('plus').toggleClass('minus');

         $(this).toggleClass('closed').toggleClass('open');

    });

});

    </script>




  </head>
};

【问题讨论】:

  • 有人用48 48替换$(
  • 尝试使用 jQuery( 而不是 $( ,有时在我的情况下我遇到了麻烦,所有 和 & 符号将被无故替换 JSF2.0 + Liferay + icefaces
  • 值得注意的是,$( 是 Perl 中的预定义变量。如果它在脚本端未转义,它将被扩展为进程的组 ID。
  • (这是一个以空格分隔的数字列表,可以重复。例如“48 48”)
  • 总是 use strictuse warnings 在您编写的每个 Perl 程序的顶部。在向他人寻求帮助之前,您至少可以做到这一点

标签: javascript jquery perl


【解决方案1】:

$( 是一个 Perl 预定义变量,扩展到进程的组 ID 列表。

如果您使用的机器同时支持多个组的成员资格,请给出您所在的组的空格分隔列表。第一个数字是 getgid() 返回的数字,随后是 getgroups() 返回的数字,一个其中可能与第一个数字相同。

有关详细信息,请参阅perlvar 文档。

为避免此问题,请确保在 Perl 字符串中转义 $((和任何其他 JavaScript $ 字符),或使用 '' 而不是 "" 以避免插值。

不好:

$html = "$(document).ready(...)";

好:

$html = "\$(document).ready(...)";
$html = '$(document).ready(...)';

以上面的代码为例,脚本部分应该是:

<script type="text/javascript">
  \$(document).ready(function () {
    \$(".closed").click(function () {

      \$(this).find("div.but").toggleClass('plus').toggleClass('minus');
      \$(this).toggleClass('closed').toggleClass('open');

    });    
  });
</script>

【讨论】:

    【解决方案2】:

    除了上面 Paul 的转义建议之外,我更喜欢使用您在上面使用的备用引用方法,这样我就不必在我的 Javascript 中转义每个字符串和引用。在 q{} 中包含一个字符串将使您不必转义每个美元符号。只要您不需要在该块中进行插值就可以了!

    所以你的代码可以写成:

    $web_content .= q{
       <script type="text/javascript">
        $(document).ready(function () {
            $(".closed").click(function () {
    
    
                 $(this).find("div.but").toggleClass('plus').toggleClass('minus');
    
                 $(this).toggleClass('closed').toggleClass('open');
    
            });
    
        });
    
        </script>
    };
    

    【讨论】:

    • 或者使用非插值的heredoc:$web_content .= &lt;&lt;'SCRIPT';
    【解决方案3】:

    由于您是通过 Perl 生成代码,$( 是 Perl 中的保留代码。把它放在一个变量中给一个字符串连接。

    文档

    从 Perl 5.6 开始,Perl 变量名可以是以控制字符(或者更好的是,插入符号)开头的字母数字字符串。这些变量必须以 ${^Foo} 的形式编写;大括号不是可选的。 ${^Foo} 表示标量变量,其名称是 control-F 后跟两个 o 。这些变量为 Perl 将来的特殊用途保留,以 ^_ 开头的变量除外(控制下划线或插入符号下划线)。任何以 ^_ 开头的控制字符名称都不会在 Perl 的任何未来版本中获得特殊含义;因此,这些名称可以在程序中安全地使用。但是,$^_ 本身是保留的。

    解决方案

    使用以下任何一种:

    "\$(document).ready(...)";
    '$(document).ready(...)';
    

    【讨论】:

    • 我以前从未写过perl!我不知道该怎么做,我想我可以在头脑中编辑东西而不会弄乱 perl 脚本。
    猜你喜欢
    • 1970-01-01
    • 2011-02-13
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多