Javascript popup link in php [duplicate] - javascript

This question already has answers here:
Quotes problem when passing value to Javascript
(2 answers)
PHP-generated javascript and quote marks
(7 answers)
javascript quotes inside quotes, string literal issue
(4 answers)
Double quote single quote not working in PHP/JavaScript [closed]
(1 answer)
Closed 4 years ago.
i have this php code:
echo "<div class='show-popup'><a class='button-popup-container-right' href='javascript:apri('popup.html');' target='_blank'><i class='far fa-play-circle'></i> ASCOLTA LA RADIO</span></a></div>"
but, when i click on the link the path is only "javascript:apri("
I have inserted this script in the footer of the page
function apri(url) {
newin = window.open(url,'Player','top=50, left=50,scrollbars=no,resizable=yes,width=400,height=550,status=no,location=no,toolbar=no,nomenubar=no');
}
Can anyone help me please?

href='javascript:apri('
The first ' marks the start of the attribute value. The second ' marks the end of the attribute value.
If you want to include ' as data then you need to either use " to delimit the attribute value or to escape the ' as &apos;.
You can avoid this problem by writing Unobtrusive JavaScript and binding your event handlers with addEventListener.

Related

How can I add additional single quotes to string in javascript and php? [duplicate]

This question already has answers here:
How to add single quote in the variable in Javascript?
(5 answers)
Closed 3 years ago.
I'm trying to build a string inside <script></script> which has a variable.
var buildurl = response.show.url;
var connectHtml = '<button class="form_button" id="connectButton" onclick="window.location='+buildurl+'">Connect</button>'
$('#someElement').html(connectHtml);
When I inspect the element, I see the url built but without the single quotes (''). So when I click on the button, it does not navigate to the url.
How can I add the '' to get
onclick="window.location='https://something.com/admin/one/two/123/456'"
?
<button class="form_button" id="connectButton" onclick="window.location=https://something.com/admin/one/two/123/456">Connect</button>
You can use template literals and not worry about apostrophes and quotes:
`<button class="form_button" id="connectButton" onclick="window.location='${buildurl}'">Connect</button>`

How do I pass a text value that contains an apostrophe to a javascript function [duplicate]

This question already has answers here:
How can I escape a single quote?
(9 answers)
Closed 5 years ago.
I'm using the onclick event to populate an input tag dynamically, the onclick is constructed server side with database values. It works great until the value I'm passing contains an apostrophe. The function never fires because the delimiters in the HTML conflict. Any help appreciated.
<span onclick='putHName("DANCER'S CALL");'>
DANCER'S CALL
</span>
function putHName(hname) {
document.getElementById("HorseName").value=hname;
document.getElementById('HorseName').focus();
}
Change your quotations around and escape the single quote using \:
onclick="putHName('DANCER\'S CALL');"

Prevent or encode ’ to ' [duplicate]

This question already has answers here:
How to replace Microsoft-encoded quotes in PHP
(6 answers)
Closed 6 years ago.
I had an issue. I do not want to insert ’ to the database.
Because when reading the ’ it will turn out with funny character.
Is there a way to prevent or encode ’ to ' when inserting to MySQL?
If you just want to replace that specific string why not just doing:
$encoded = str_repace("’","'",$input);

regex for stripping javascript on PHP [duplicate]

This question already has answers here:
remove script tag from HTML content
(13 answers)
Closed 7 years ago.
I want to delete all javascript found on a string with PHP, I'm trying to do it trough preg_replace like this: $text = preg_replace("/<script.*<\/script>/mis", "", $text);
But after doing this, $text is an empty string. What I'm doing wrong? Trying this regex on http://www.phpliveregex.com/ it seems to work, but using it in real life returns me nothing.
Typically you do not just want to remove (escape) javascript but all sorts of HTML tags which prevent a vulnerability to you. You can do so using
http://docs.php.net/manual/en/function.htmlspecialchars.php
or
http://docs.php.net/manual/en/function.htmlentities.php
Then you dont have to bother with regex altogether.

JavaScript variable containing line breaks [duplicate]

This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 8 years ago.
I have text containing line breaks from a TextArea stored in a database. A php script tries to retrieve this text and store it in a JavaScript variable. The problem is that the string is not properly understood by JavaScript and it is interpreted that JavaScript variable is not correctly ending because JavaScript assumes a line break.
Any idea how could I save text containing line breaks in a JavaScript variable?
Thanks in advance.
Any idea how could I save text containing line breaks in a JavaScript
variable?
Yes, Use PHP's json_encode:
var myvar = <?php echo json_encode($myVarValue); ?>;

Categories