Javascript unescape doesn't seem to work [duplicate] - javascript

This question already has answers here:
Unescape apostrophe (') in JavaScript?
(2 answers)
Closed 8 years ago.
I have a simple string which is
Company's
Now I have some javascript which is ran when a form is submitted
var jsCompanyName = '#Model.Name';
var unescapedCompanyName = unescape(jsCompanyName);
$('.selector-input').val(unescapedCompanyName);
$('.selector-input-id').val('#Model.Id');
Going thought with a debugger, my var unescapedCompanyName is still "Company's" even after the unescape function, does anyone have any idea on why this isn't removing ' and replacing it with a '

The unescape() function has nothing to do with HTML syntax. It's for handling escapes in URL syntax, which is a completely different thing. (It's also deprecated even for its intended purpose.)
There's no built-in function to deal with HTML escapes. However, code running in a web browser can do something like this:
function html_unescape(s) {
var div = document.createElement("div");
div.innerHTML = s;
return div.textContent || div.innerText; // IE is different
}

You can do this easily with JQUERY if you really need to:
function htmlDecode(value) {
return $('<div/>').html(value).text();
}
var str = 'Company's';
console.log(htmlDecode(str)); // Company's
JSFIDDLE.

Related

How are +variable+ and ${variable} different in javascript? [duplicate]

This question already has answers here:
ES6 template literals vs. concatenated strings
(4 answers)
Closed 1 year ago.
I'm start coding.
While I was watching a lecture, I saw code like this.
var coworkers = ['go', 'hello', 'hi', 'doit'];
<script type="text/javascript">
var i = 0;
while(i < coworkers.length){
document.write('<li>'+coworkers[i]+'</li>');
i = i + 1;
}
But But when I searched, Said to use ${variable} in JavaScript. and It didn't work.
How are +variable+ and ${variable} different in javascript? Thanks :)
Those placeholders, like ${variable}, can only be used in template literals, which are always enclosed in backticks(the key right below you escape key).
If it still doesn’t work, maybe you’re using an older browser?

Is this a good string sanitizer? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
HtmlSpecialChars equivalent in Javascript?
I couldn't find a good string sanitization function to be safely used inside HTML. I was wondering if this is a good approach:
String.prototype.sanitize = function() {
return $('<div></div>').text(this).html();
}
For sanitizing against XSS, yes. For sanitizing against SQL injections, no.
It's better (and still easy) to remove the jquery requirement:
String.prototype.htmlspecialchars = function() {
var span = document.createElement('span'),
txt = document.createTextNode(this);
span.appendChild(txt);
return span.innerHTML;
}
The coupling with document still isn't so bad, because that's where it's going to be used anyway, but I prefer using successive String.replace() like in this answer.

Simple Javascript Replace not working [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 3 years ago.
This seems so simple and trivial but it is not working. Here is my javascript:
var url = "/computers/";
console.log(url);
url.replace(/\//gi, " ");
console.log(url);
And here is the output in my browsers console:
/computers/
/computers/
As you can see nothing changes. As you can tell from the code I'm trying to replace the forward slashes with spaces. What am I doing wrong?
url = url.replace(/\//gi, " ");
Nothing changes because you're not assigning the result of the replacement to a variable. Add url = url.replace()
url.replace(/\//gi, " "); returns the resulting string (in javascript you can't modify an existing string), you are not assigning it to anything
assign it like so:
url = url.replace(/\//gi, " ");

Replace function not replacing [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I followed some documentation to use the JavaScript replace function and it's not changing anything. No errors are thrown. Any idea what I'm doing wrong? The variable is retrieved from XML - maybe it needs to be cast as a string or something?
for (var i = 0, iln = projects.length; i < iln; i++){
var thumb = projects[i].get('thumb');
thumb.replace("200.jpg", "640.jpg");
console.log(thumb) //200.jpg not replaced
}
The full thumb value should look like this:
http://b.vimeocdn.com/ts/160/895/160895498_200.jpg
Is there a better way to find and replace things?
Assign the value back into thumb.
thumb = thumb.replace("200.jpg", "640.jpg");
Try:
thumb = thumb.replace("200.jpg", "640.jpg");

Is there a += available for attr() and val()? [duplicate]

This question already has answers here:
Is it possible to do ".value +=" in JQuery?
(5 answers)
Closed 8 years ago.
Since I've moved to jQuery development has sped up for a great deal, but there is one thing which has become a little more time-consuming: appending some string to an attribute/value.
For example, if you wanted to append a letter to the value of a textbox, you would normally do:
input.value += 'a';
but now using jQuery, it has become:
input.val(input.val() + 'a');
I know it wouldn't be too hard to create a function which can do this and then append that function to jQuery.fn, but I was rather wondering whether there is such a function perhaps already available. I haven't found any so far. Or is there perhaps a better technique of appending a string to a value/attribute than what I'm doing at the moment?
If you know that your element has a value attribute, there is no reason not to use
input.value += 'a';
There is nothing to stop you using plain JavaScript in your jQuery scripts.
In fact,
input.val(input.val() + 'a');
has no benefits over using plain 'js', except if your input is a jQuery object and you want to chain methods.
I'd suggest overall
var $input = $('#someInput');
$input[0].value += 'a'; //get the DOM object from your jQuery object

Categories

Resources