« Prev 1 2 3 4 5 6 7 8 9 10 Next »

Building web & mobile apps with ionic, capacitor, and react

Create Date: May 31, 2019 at 12:51 PM         Tag: REACT         Author Name: Sun, Charles

Ely Lucas @ Denver Dev Day

how it works:

apps built with inoic (open source)

npm install -g ionic
ionic start myapp tabs--type=react
ionic serve

 

 

New Comment

Apostrophe (') problem in javascript

Create Date: May 14, 2019 at 12:29 PM         Tag: JAVASCRIPT         Author Name: Sun, Charles

in my case I just use replace in javascript to pass the string to C#.

JSON.stringify(res).replace(/'/g, "'");

in the C# code, I need to use replace again

.Replace("'", "\'")

Unescape apostrophe (') in JavaScript?

unescape has nothing to do with HTML character entities. It's an old, deprecated function for decoding text encoded with escape, which is an old, deprecated function for encoding text in a way that is unlikely to be useful in the modern world. :-)

If you need to turn that HTML into plain text, the easiest way is via an element:

var div = document.createElement('div');
div.innerHTML = "'";
alert(div.firstChild.nodeValue);

Live Example | Live Source

Note that the above relies on the fact that there are no elements defined in your HTML text, so it knows there is exactly one child node of div, which is a text node.

For more complicated use cases, you might use div.innerText (if it has one) or div.textContent:

var div = document.createElement('div');
div.innerHTML = "'";
alert(div.innerText || div.textContent || "");

Apostrophe problem in javascript

<script language="javascript">
function myFunction(strID , strName)
{
strName = strName.replace(/'/g,"\'"); // escape all apostrophes

document.form1.HiddenID.value = strID
document.form1.HiddenName.value = strName

window.opener.document.form1.ClientID.value = document.form1.HiddenID.value
window.opener.document.form1.ClientName.value = document.form1.HiddenName.value
window.close()
}
</script>

 

New Comment

addEventListener using for loop and passing values

Create Date: May 03, 2019 at 02:32 PM         Tag: JAVASCRIPT         Author Name: Sun, Charles

addEventListener using for loop and passing values [duplicate]

Closures! :D

This fixed code works as you intended:

for (var i = 0; i < elem.length; i += 2) {
    (function () {
        var k = i + 1;
        var boxa = elem[i].parentNode.id;
        var boxb = elem[k].parentNode.id;

        elem[i].addEventListener("click", function() { makeItHappen(boxa,boxb); }, false);
        elem[k].addEventListener("click", function() { makeItHappen(boxb,boxa); }, false);
    }()); // immediate invocation
}

Why does this fix it?

for(var i=0; i < elem.length; i+=2){
    var k = i + 1;
    var boxa = elem[i].parentNode.id;
    var boxb = elem[k].parentNode.id;

    elem[i].addEventListener("click", function(){makeItHappen(boxa,boxb);}, false);
    elem[k].addEventListener("click", function(){makeItHappen(boxb,boxa);}, false);
}

Is actually non-strict JavaScript. It's interpretted like this:

var i, k, boxa, boxb;
for(i=0; i < elem.length; i+=2){
    k = i + 1;
    boxa = elem[i].parentNode.id;
    boxb = elem[k].parentNode.id;

    elem[i].addEventListener("click", function(){makeItHappen(boxa,boxb);}, false);
    elem[k].addEventListener("click", function(){makeItHappen(boxb,boxa);}, false);
}

Because of variable hoisting, the var declarations get moved to the top of the scope. Since JavaScript doesn't have block scope (forifwhile etc.) they get moved to the top of the function. Update: as of ES6 you can use let to get block scoped variables.

When your code runs the following happens: in the for loop you add the click callbacks and you assign boxa, but its value gets overwritten in the next iteration. When the click event fires the callback runs and the value of boxa is always the last element in the list.

Using a closure (closing the values of boxaboxb etc) you bind the value to the scope of the click handler.

New Comment
« Prev 1 2 3 4 5 6 7 8 9 10 Next »