HTML excerpt as a parameter of a javascript function

0

I have little knowledge of javascript, how can I do to pass a video incorporation code into a function as a string?

<a href="javascript:void(0)" onclick="exibirVideo('CODIGO DE EMBED AQUI')">

I'm having problems because I can not pass the embed of the video as a string, which ends up closing the <a> tag prematurely with some of the embedded%     

asked by anonymous 13.09.2016 / 20:49

1 answer

1

If you set the anchor "click" callback to <script*src=*>*</script> you will never have to deal with script-inline problems.

Your problem must be quoted, you can solve using () quotes if the (') apostrophes used to represent a value, or vice versa.

If you want to represent a quotation mark or apostrophe in a string in JavaScript you can escape it with \, for example: <a onclick='"\' apóstrofo - \" aspa"'></a> . Or:

"\x22" // aspa (ASCII)
String.fromCharCode(0x22) // aspa
String.fromCharCode(34) // aspa
"\x27" // apóstrofo (ASCII)
"\u0022" // aspa (UTF-8)
"\u0027" // apóstrofo (UTF-8)

Basic example of using the <script/> tag.

<script>
document.querySelector('a').onclick = function() {
    exibirVideo('\
                Embed code')
}
</script>
    
14.09.2016 / 15:40