简体   繁体   中英

Echo JavaScript

When I try to echo JavaScipt code with normal text like this:

echo '<script type="text/javascript">document.getElementById("errorbox").innerHTML = "Please enter a valid invite link!";</script>';

It works fine, but when I try to echo something that uses an user input:

echo '<script type="text/javascript">document.getElementById("errorbox").innerHTML = "Your New Link: <a href="' . $link . '">' . $link . '</a> (websi.te/' . $link . ')";</script>'; 

It doesn't work, and I get this error:

SyntaxError: unexpected token: identifier

Your quotes didn't match. I checked your code in PhpStorm, and after replacing and escaping two quotes in the JavaScript string it din't show a syntax error anymore.

I changed

"Your New Link: <a href="' . $link . '">' . $link . '</a> (websi.te/' . $link . ')"

to

\'Your New Link: <a href="' . $link . '">' . $link . '</a> (websi.te/' . $link . ')\'

complete string:

echo 
    '<script type="text/javascript">' . 
        'document.getElementById("errorbox").innerHTML = \'Your New Link: <a href="' . $link . '">' . $link . '</a> (websi.te/' . $link . ')\';' . 
    '</script>';

I guess the double quote is wrong. Try to this. I edited <a href=\\" ...

Good luck.

echo '<script type="text/javascript">document.getElementById("errorbox").innerHTML = "Your New Link: <a href=\"' . $link . '\">' . $link . '</a> (websi.te/' . $link . ')";</script>'; 

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