Note that there are some explanatory texts on larger screens.

plurals
  1. POchecking for undefined variable with Underscore.js
    primarykey
    data
    text
    <p>I'm having a problem with Underscore.js templates and Internet Explorer. Here's part of the template which is causing trouble:</p> <pre><code>&lt;p&gt; &lt;% if ( typeof description !== 'undefined' ) { %&gt; &lt;%- description %&gt; &lt;% } else { %&gt; No description &lt;% } %&gt; &lt;/p&gt; </code></pre> <p>When the variable <code>description</code> is undefined (which means I'm not supplying it to the template at all, the variable does not exist), this works just fine in Safari, Firefox, Chrome. </p> <p>Internet Explorer however, doesn't work correctly. Instead of showing <code>No description</code> IE8 and IE9 show <code>[object HTMLMetaElement]</code>, and IE7 shows <code>[object]</code>. </p> <p>Checking the result of <code>typeof description</code> returns <code>undefined</code> in Safari, Firefox, Chrome, but apparently Internet Explorer returns <code>object</code> instead. </p> <p>I already tried Underscore.js's <code>_.isUndefined(value)</code> function, but that one doesn't work when the variable does not exist. </p> <p><strike>Does anyone know a workaround for this problem? (note that I am unable to supply the variable with no value - it either exists, or it doesn't)</strike></p> <p><strong>Update</strong> I found a workaround in one of the Underscore.js Github issues <a href="https://github.com/documentcloud/underscore/issues/237#issuecomment-1781951" rel="nofollow">https://github.com/documentcloud/underscore/issues/237#issuecomment-1781951</a> </p> <p>Can someone explain why IE behaves differently, and why the workaround actually works?</p> <p><strong>Update 2</strong> @John-DavidDalton has provided another, better workaround in the comments below (linking directly to it doesn't seem to work)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload