【问题标题】:window.open not working parse URL truncatedwindow.open 不工作解析 URL 被截断
【发布时间】:2018-10-14 19:02:10
【问题描述】:

我正在尝试获取一个在 PHP 中作为新窗口打开的链接,我尝试了以下方法和一些变体,但由于某种原因,链接停止在 javascript:void(window.open(
任何的想法? '""' 会不会有问题?

$html .= '<a class="ficha_partido_popup" href="javascript:void(window.open("/servicios/deporte/partidos/fichapartido_'.$filaPartido["partidocod"].'.html"))" rel="nofollow" title="Ver Ficha del partido"><img src="/imagenes/ficha.png" alt="Ver la ficha del partido" /></a>';

【问题讨论】:

    标签: javascript php url window.open


    【解决方案1】:

    你需要转义双引号:

    $html .= '<a class="ficha_partido_popup" href="javascript:void(window.open(\"/servicios/deporte/partidos/fichapartido_'.$filaPartido["partidocod"].'.html\"))" rel="nofollow" title="Ver Ficha del partido"><img src="/imagenes/ficha.png" alt="Ver la ficha del partido" /></a>';
    

    【讨论】:

    • 嗨!感谢您的回复...我尝试了此解决方案,但在线结果是: 任何想法?
    • 取决于您使用的“在线”工具,可能会发生各种转义。在您自己的环境中使用它。
    【解决方案2】:
    • 您正在使用'(单引号)在 PHP 中创建字符串。
    • 您正在使用 "(双引号)在 Javascript 中创建字符串。

    现在,您说的是href="...window.open("...")"。问题是您试图将" 嵌套在" 中,而这会破坏这一切。因此 window.open 中的第一个双引号最终成为 href 的结束双引号,而字符串的其余部分在 javascript 中变得无效。

    要解决这个问题,你可以用单引号替换嵌套的双引号并说href="...window.open('...')",但你不能这样做,因为如果你在这里使用单引号,你最终会破坏 PHP 中的东西,因为你正在使用用单引号形成 PHP 中的字符串。

    所以,使用单引号但将它们转义,就像这样 -

    href="...window.open(\'...\')"
    

    因此,您的代码现在将变为 -

    $html .= '<a class="ficha_partido_popup" href="javascript:void(window.open(\'/servicios/deporte/partidos/fichapartido_'.$filaPartido["partidocod"].'.html\'))" rel="nofollow" title="Ver Ficha del partido"><img src="/imagenes/ficha.png" alt="Ver la ficha del partido" /></a>';
    

    【讨论】:

      【解决方案3】:

      为简单起见,分成2行

      $window_link = 'window.open("/file_path/filename_'.$filaPartido["partidocod"].'.html")';
      $html .= '<a href="javascript:void( ' . $window_link . ' )" >Any link</a>';
      

      $link = $filaPartido["partidocod"];
      $html .= <<<HTML
      <a href="javascript:void( window.open('/file_path/filename_{$link}.html') )" >Any link</a>
      HTML;
      

      【讨论】:

      • 第二个选项就像一个魅力!非常感谢你!!!最好的问候!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2019-04-25
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多