Pregunta

See the code below:

<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
    <td>
        <span class="preview">
            {% if (file.thumbnailUrl) { %}
                <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
            {% } %}
        </span>
    </td>
    <td>
        <p class="name">
            {% if (file.url) { %}     //I need to check here
            <a class="btn btn-primary download"  href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>
                <i class="glyphicon glyphicon-folder-close"></i>
                </a>
                                    <span>{%=file.name%}</span>
            {% } else { %}
                <span>{%=file.name%}</span>
            {% } %}
        </p>
        {% if (file.error) { %}
            <div><span class="label label-danger">Error</span> {%=file.error%}</div>
        {% } %}
    </td>
</tr>
{% } %}
</script>

Now, I am checking it as a URL or not. But I want to check, whether that URL has a file extension or not. How to give the if condition?

¿Fue útil?

Solución

Did you post the wrong code? The following function will return true is the URL passed to it has a path beyond the domain name and if that path has a period.

function hasExt( url ) {
    var parts = url.split('/'),
        last  = parts.pop();
    return ( parts.length > 3 ) && ( last.indexOf('.') != -1 );
}

Example: url = 'http://example.com/index.html' will return true but url = 'http://example.com/' will return false

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top