JavaScript variable containing line breaks [duplicate] - javascript

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); ?>;

Related

Javascript popup link in php [duplicate]

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.

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');"

Get text between tags regex javascript [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 4 years ago.
I am trying to get the text between the two tags which also handles multiple lines.I have managed to do it in PHP regex but i am stuck on the javascript one.
<any_tag>random text
dffdffdfdfdfdfdfdfd
dfdfdfdfdfdfdfdfdf
</any_tag>
I want to get the text between <any_tag> and </any_tag>
This is the regex for php
https://regex101.com/r/tQ1bX7/2
Now i am trying to achieve to same in javascript
Give try on following :
/[^(<any_tag>)](?:.*(\r?\n?).*)*(?=\<\/any_tag\>)/gi
Explination :
[^(<any_tag>)] match a single character not present in the list below
(<any_tag>) a single character in the list (<any_tg>) literally (case insensitive)

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 string split for string containing backslashes [duplicate]

This question already has answers here:
Need a basename function in Javascript
(19 answers)
Closed 8 years ago.
I have a string like C:\Users\shail.jet\Desktop\cca-lan_test_cases_path.txt.
I want to get only file name that is in this case is cca-lan_test_cases_path.txt.I have tried it with javascript split function but it did'nt work. Any help will be appreciated.
"C:\\Users\\shail.jet\\Desktop\\cca-lan_test_cases_path.txt".split("\\").pop();
Make sure you add escape backslashes in the file path string otherwise javascript will ignore it.
That will split by backslash and then use pop to get the last element in the array which will be the file name.
var filename = fullPath.replace(/^.*[\\\/]/, '')
This will handle both \ OR / in paths
var filename = fullPath.replace(/^.*(\\|\/|\:)/, '');
should also protect from empty string

Categories