简体   繁体   中英

window.open not working parse URL truncated

I'm trying to get a link to open as a new window in PHP, I tried the following and a few variations, yet for some reason the links stops in javascript:void(window.open(
Any idea? Could it be something wrong with the '""' ?

$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>';

您需要转义双引号:

$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>';
  • You're using ' (single quote) to create the string in PHP.
  • You're using " (double quote) to create the string in Javascript.

Now, you're saying href="...window.open("...")" . The issue is that you're trying to nest " within " and that just breaks it all. So the first double quote inside window.open ends up being the closing double quote for href and the rest of the string just becomes invalid in javascript.

To fix this, you can replace nested double quotes with single quotes and say href="...window.open('...')" , except that you can't since if you use single quotes here, you'll end up breaking things in PHP since you're using single quotes to form the string in PHP.

So, use single nested quotes but escape them, like this -

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

So, your code will now become -

$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>';

To make it simple, divide it into 2 lines

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

OR

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM