View Full Version : Multiple names for function - legal?

Jul 26th, 2012, 08:33 AM
Hi all,

I have a little function that I continually mix up the name for:

* strip all html super duper foolproof regex (sure!)
* http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx
String.prototype.stripHTML = String.prototype.stripHtml = function() {
var r = /<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|\'.*?\'|[^\'">\s]+))?)+\s*|\s*)\/?>/gi
return this.replace(r, '');
I either type xx.stripHtml() or xx.stripHTML() and then have to re-remember why my code doesn't work! :)

So, I did "this name" = "that name" = function.

It seems to work, but question: Is it legal code?


-- Roger

Calvert Tripesi
Jul 26th, 2012, 11:01 AM
If it were illegal, the error console would tell you. Of course if you were in the habit of using the console, you wouldn't have the problem that you're trying to avoid.

Jul 27th, 2012, 07:55 AM
if it were not for the <script> tags (and IE), a combination of .innerHTML and .textContent seemed easier ...

Old Pedant
Jul 28th, 2012, 04:21 AM
Well, true, Dormilich. But what about just doing it this way:

<div id="foo">
<b>Now is <a href="xyz.html">test</a> for </b> all <span>this</span>
<script type="text/javascript">
var d = document.getElementById("foo");
alert( d.textContent == null ? d.innerText : d.textContent );


Jul 29th, 2012, 10:31 AM
that would be a standard workaround for the IE problem. but I’m not sure, if it would close attack vectors through <script>.

though I’d rather use a slightly different condition:

alert( "textContent" in d ? d.textContent : d.innerText );