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 8644381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:16:49+00:00 2026-06-12T12:16:49+00:00

<script type=text/javascript> $(window).resize(function(){ var width = $(window).width(); if (width < 361) { $(.infograph-image).attr(src,/images/infographicHowMobile.png); }

  • 0
<script type="text/javascript">
  $(window).resize(function(){
  var width = $(window).width(); 

   if (width < 361) {
      $(".infograph-image").attr("src","/images/infographicHowMobile.png");
   }
 }); 
</script>

I wish to change the image source of a given image, based on the viewport width size.
If the viewport width size is 360 or less, change to a mobile version of the image.

I have two simple questions:

1)
How can we do both: a) detect windows resize AND document ready ?

This, I believe I got:

I need to change that into a function.
Then call it on load;

checkResponsive();

Then bind a event listener:

   

$(window).resize(checkResponsive);

2)
We have several images that need to have a mobile version, should this be converted to a function ? Or several ifs may work ?

Can you please give me a kick-off start on those two questions please.

  • 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-06-12T12:16:50+00:00Added an answer on June 12, 2026 at 12:16 pm

    Create a seperate function and call them on both events.
    So, for example, create the function mobileImg() like this:

    function mobileImg(targetClass, imageSrc) {
        var width = window.innerWidth; // No need for jQuery here, raw JS can do this
    
        if(width < 361) {
            $(targetClass).attr("src", imageSrc);
        }
    }
    

    And then call this on both events.

    $(document).ready(mobileImg(".infograph-image", "/images/infographicHowMobile.png"));
    $(window).resize(mobileImg(".infograph-image", "/images/infographicHowMobile.png"));
    

    You can then call the mobileImg method as much as you want and with whatever params.
    If you really want to make it clean, also add a check if the passed element and image exist at all and use fallbacks where needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my code: <script type=text/javascript> (function() { window.PinIt = window.PinIt || { loaded:false };
<div id="myLuckyPost"> </div> <script type="text/javascript"> function showLucky(root){ var feed = root.feed; var entries =
Here is fragments of my HAML view: %script{:type => text/javascript, :src => assets/application.js} :javascript
#!/usr/bin/perl print Content-type: text/html \n\n; print qq(<html> <head> <script type=text/javascript src=/jquery.js></script> <script> jQuery(document).ready(function(){ jQuery(#form_lang).submit(function(e){
I have this code <html> <head> <script src=jquery-1.7.2.min.js type=text/javascript></script> <script src=jquery-ui-1.8.20.custom.min.js type=text/javascript></script> <script type
This my javascript :- <script type =text/javascript> $(function() { $(#tabs).tabs({ cache: true ,fx: {
Look this code: <script type = text/javascript> function mouseClick (container) { container.appendChild (document.createTextNode (Can
<?php $_SESSION['dsize'] = $_POST['dsize']; if ($_SESSION['deadline']==Urgent ) { ?> <script type = text/javascript> window.open('https://usd.swreg.org/cgi-bin/s.cgi?s=104597&p=104597-1&v=0&d=0&q=<?php
I have a jQuery script: $.ajax({ url: /scripts/secure/development/ajax.asp, type: POST, dataType: text, data: cmd=addresses,
I have this jQuery script var dataString = class_id=+class_id; $.ajax({ type: POST, url: page.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.