Author | Message | Time |
---|---|---|
Grok | Why does innerHTML append a space when there is none in reality? <li id="li_debug">ExtraSpace</li> <SCRIPT language="javascript"> alert('innerHTML is (' + document.getElementById('li_debug').innerHTML + ')'); </SCRIPT> This seriously messes with my ability in dynamic HTML if I depend on the innerHTML to locate elements. | March 11, 2004, 5:01 PM |
Myndfyr | [quote author=Grok link=board=22;threadid=5730;start=0#msg48927 date=1079024505] Why does innerHTML append a space when there is none in reality? <li id="li_debug">ExtraSpace</li> <SCRIPT language="javascript"> alert('innerHTML is (' + document.getElementById('li_debug').innerHTML + ')'); </SCRIPT> This seriously messes with my ability in dynamic HTML if I depend on the innerHTML to locate elements. [/quote] So just create a trim() function (I checked, there isn't an implicit one on String types). It wouldn't be hard. | March 11, 2004, 6:41 PM |
Grok | Edit: Still broken. InnerHTML and InnerText both show the extra character. | March 11, 2004, 6:44 PM |
Grok | Hmm JavaScript has much more ability than I had known. | March 11, 2004, 8:03 PM |
Grok | Ended up writing a trim function from regular expressions. function trim(e) { return e.replace(/(^\s*)|(\s*$)/g, ""); } The regex expression was in an MSDN example showing how to add a trim method to your own object. | March 11, 2004, 8:11 PM |
St0rm.iD | innerHTML is bad. Don't use it if you can avoid it. | March 11, 2004, 9:55 PM |
Grok | [quote author=St0rm.iD link=board=22;threadid=5730;start=0#msg48957 date=1079042107] innerHTML is bad. Don't use it if you can avoid it. [/quote] Explain? And answer this please. Can't find it in my Wrox Professional HTML 4.01 Reference. #include "/js/MyLibrary.js" Do I use <link> to get server to include my javascript function library? Or what? | March 11, 2004, 10:12 PM |
St0rm.iD | [code] <script src="bleh.js"></script> [/code] innerHTML isn't standard in every DOM implementation...I think. | March 12, 2004, 1:55 AM |
Myndfyr | No it's not; innerHTML is part of the Internet Explorer DOM. The Netscape's document.layers model does not have something like that (well I'm sure it does, but I can't remember). If you remember the property, you can use function prototyping for the same effect. It's been a long time, but in dealing with reality, I've never really worried about making it cross-browser friendly. I looked at my site statistics to discover that 99.5% of my site's hits were IE (the one right now that uses ImageReady cross-platform JS for image rollovers). Remember though, you can get away with different code type for different browsers: [code] var ns4 = ie4 = other = false; if (document.layers) { ns4 = true; } else if (document.all) ie4 = true; } else { other = true; } if (ns4) { // do netscape-compatible junk } else if (ie4) { // do microshaft stuff } else { // say "I suck cuz I don't use a standard browser! } [/code] IIRC, document.layers isn't a part of the Mozilla spec, either. | March 12, 2004, 3:23 AM |