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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:36:47+00:00 2026-06-17T11:36:47+00:00

How to switch an image when the page refreshes, using Javascript? Let’s say I

  • 0

How to switch an image when the page refreshes, using Javascript?

Let’s say I have 2 images:

  • ImageA.jpg
  • ImageB.jpg

I want to switch those images at locationA and locationB when the page is refreshed.

Simulation:

- Page Refresh #1
<img id='locationA' src='ImageA.jpg'>
<img id='locationB ' src='ImageB.jpg'>


- Page Refresh #2
<img id='locationA' src='ImageB.jpg'>
<img id='locationB ' src='ImageA.jpg'>


- Page Refresh #3
<img id='locationA' src='ImageA.jpg'>
<img id='locationB ' src='ImageB.jpg'>

[Update #1]

I try this implementation, but it doesn’t work. Could anyone tell me whats wrong with this code?

<html>
  <head>
    <script type="text/javascript">
      var images = [];
      images[0] = "I_am_Super_Magnet%21.jpg";
      images[1] = "World_In_My_Hand%21.jpg";

      var index = sessionStorage.getItem('index');
      if(index) index = 0;

      if(index==0)
      {
        document.getElementById("locationA").src=images[index];
        document.getElementById("locationB").src=images[index+1];
        index = index + 1;
      }
      else if(index==1)
      {
        document.getElementById("locationA").src=images[index];
        document.getElementById("locationB").src=images[index-1];
        index = index - 1;
      }
     sessionStorage.setItem('index', index);
    </script>
  </head>
  <body>
    <img id='locationA' src=''>     
    <img id='locationB' src=''>
  </body>
</html>

[Update #2]

Tested on:

  • FF 16.0.1 –> Working!
  • IE 8 –> doesn’t work

Here is the code:

<html>
  <head>
    <script type="text/javascript">
    function switchImage()
    {
      var images = [];
      images[0] = "I_am_Super_Magnet%21.jpg";
      images[1] = "World_In_My_Hand%21.jpg";

      var index = sessionStorage.getItem('index');

      if(index == null) index = 0;//set index to zero if null

      index = parseInt(index);// parse index to integer, because sessionStorage.getItem() return string data type.

      if(index == 0)
      {
        document.getElementById("locationA").src=images[index];
        document.getElementById("locationB").src=images[index+1];
        index = index + 1;
      }
      else if(index == 1)
      {
        document.getElementById("locationA").src=images[index];
        document.getElementById("locationB").src=images[index-1];
        index = index - 1;
      }
      sessionStorage.setItem('index', index);
    }
    </script>
  </head>
  <body onload="switchImage()">
    <img id='locationA' src='src_locationA'>        
    <img id='locationB' src='src_locationB'>
  </body>
</html>

Thanks to Jack for the
clue! and thanks to Jon Kartago
Lamida
for
the sample!

Thanks.

  • 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-17T11:36:48+00:00Added an answer on June 17, 2026 at 11:36 am

    Base on Damien_The_Unbeliever comment I create this answer post for my own question.

    This is final working solution that I use.

    [Update #3]

    Tested on:

    • FF 16.0.1 –> Working!
    • IE 8 –> Working!
    • Chrome 24 –> Working! (Note: this browser need a little extra effort to make it can read cookie. see this link)

    Basically the code is still same as Update #2, the different is I use cookie instead of sessionStorage. Here is the complete code:

    <html>
      <head>
        <script type="text/javascript">
        function createCookie(name,value,days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = name+"="+value+expires+"; path=/";
        }
    
        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }
            return null;
        }
    
        function eraseCookie(name) {
            createCookie(name,"",-1);
        }
    
        function switchImage()
        {
          var images = [];
          images[0] = "I_am_Super_Magnet%21.jpg";
          images[1] = "World_In_My_Hand%21.jpg";
    
          var index = readCookie('index'); //sessionStorage.getItem('index');
    
          if(index == null) index = 0;//set index to zero if null
    
          index = parseInt(index);// parse index to integer, because sessionStorage.getItem() return string data type.
    
          if(index == 0)
          {
            document.getElementById("locationA").src=images[index];
            document.getElementById("locationB").src=images[index+1];
            index = index + 1;
          }
          else if(index == 1)
          {
            document.getElementById("locationA").src=images[index];
            document.getElementById("locationB").src=images[index-1];
            index = index - 1;
          }
          createCookie('index', index); //sessionStorage.setItem('index', index);
        }
        </script>
      </head>
      <body onload="switchImage()">
        <img id='locationA' src='src_locationA'>        
        <img id='locationB' src='src_locationB'>
      </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web page where I want to switch the background image of
I want to switch images on .click() using the url attr I can do
I want to switch an image under Facebook page's tab (static FBML application), but
I'm using JQuery to switch out an image src thusly: $(#myImg).attr(src, ../../new.gif); notice the
How to automatically switch the background in layout android:background=@drawable/image> I have two pictures I
I've made a custom image for a checkbox and want it to switch between
I have three images: the first is a light switch, the second is an
Is it possible to switch HTML on click? I have an index page with
I m using jquerymobile, I want to put content inside the page fetched by
Is it possible to have one php page that uses a switch to show

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.