Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6775061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:51:44+00:00 2026-05-26T15:51:44+00:00

I have a url in javascript, set as a variable. Like: var imageurl=’example.com/test/real/image.jpg’; I

  • 0

I have a url in javascript, set as a variable. Like:

var imageurl='example.com/test/real/image.jpg';

I am trying to recognize if this is an image url or not (ending with jpeg, jpg, gif, png etc.).

I tried using charAt but it did not work.

What’s the best way to return a 1 if it is an image url and 0 otherwise using javascript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:51:44+00:00Added an answer on May 26, 2026 at 3:51 pm

    I am trying to recognize if this is an image url or not (ending with jpeg, jpg, gif, png etc.).

    The file extension (if there even is one) has no bearing on whether a URL points to a resource that is an image type. That is only a convention used by some people for static resources.

    Looking at the file extension can be enough if you only need it as a general heuristic:

    // User browser's built-in URL parsing to discard query string etc
    //
    var a= document.createElement('a');
    a.href= url;
    var ext= a.pathname.split('.').pop().toLowerCase();
    var mightbeimage= ext=='gif' || ext=='jpeg' || ext=='jpg' || ext=='png';
    

    but for accuracy the only way to find out if a URL points to an image is to fetch that URL and see what comes back. Typically you’d do that by issuing a HEAD request to the URL and seeing if the headers in the response contain Content-Type: image/something.

    You can’t do that directly from browser JavaScript, but what you can do is create an image element and see if loads OK:

    function checkImage(url, callback) {
        var img= new Image();
        img.onload= function() { callback(true); }
        img.onerror= function() { callback(false); }
        img.src= url;
    }
    
    checkImage('http://www.google.co.uk/images/srpr/logo3w.png', function(isimage) {
        if (isimage)
            alert('Yes, it is an image');
        else
            alert('Nope, not an image, or does not exist or other error');
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have URL like: http://example.com#something , how do I remove #something , without causing
I have URL scheme for my blog like this: http://www.example.com/%YEAR%/%MONTH%/%CATEGORY%/%POST_TITLE%/ Now i want to
I have a url string like this: http://www.google.com/cse?cx=017840637368510444960:ny1lmky7r-0&client=google-csbe&output=xml_no_dtd&q=simon+g I need to send this url
I need a javascript bookmark to take the url I have in the clipboard
I have the following javascript: $.ajax({ type: "POST", dataType: "json", url: "/Home/Submit", data: {
I have a hard coded URL like so: https://bupacouk.bwa.local.internal.bupa.co.uk/cash-plan-quote/quoteAction.do?getBenefitLevelDetails=getBenefitLevelDetails&productPolicyId=7841#a1 When Javascript is enabled i
I have the following code: <script type=text/javascript> $(document).ready(function() { $(#Save).click(function() { $.post(url, { data:
I have a JavaScript file which has a hard coded BASEURL variable, this value
I'm using Fancybox and Zend. Hope the following is clear. On www.example.com/template/edit/id/3 I have
I have this bit of javascript: var jsonString = some string of json; $.post('proxy.php',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.