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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:35:14+00:00 2026-06-03T14:35:14+00:00

This is my first question, hence please ignore mistakes, if any. I have an

  • 0

This is my first question, hence please ignore mistakes, if any.

I have an issue where I am serving three alternate layouts to my visitors based on the device they are surfing from.

Mobile, Tablet, and Desktop.

My website is in PHP and I am using just 280 bytes of JavaScript to determine the browser width of my visitor. There is no other JavaScript or any libraries like jQuery, MooTools. I want to keep my site very very light weight thus resulting in faster page load speeds.

I have a PHP variable called $layout and the the value to it is assigned by the JavaScript dynamically based on the browser width. The three values assigned to it are mobile or tablet or desktop

Now I have links in my xhtml which are like this:

<img src="cdn/images/desktop/image1.jpg" width="500" height="200" alt="image1">
<img src="cdn/images/desktop/image2.jpg" width="500" height="200" alt="image2">
<img src="cdn/images/desktop/image3.jpg" width="500" height="200" alt="image3">

By default images are loading from the cdn/images/desktop folder.

What I am looking for is if the value is $layout is tablet then the images should load from cdn/images/tablet folder and similarly if the valur of $layout is mobile then images should load from cdn/images/mobile folder.

Name of the images remain the same. They are three different resolutions in three different folders.

Kindly suggest a PHP solution if possible to do this in PHP.

Otherwise please suggest a plain JavaScript solution (No libraries like jQuery, MooTools ot any such)

Thanks

UPDATE

Actually I am using Joomla as a CMS so in my posts I cannot use PHP code within posts hence I want that after the page is rendered or during the rendering these paths must change.

  • 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-03T14:35:16+00:00Added an answer on June 3, 2026 at 2:35 pm
    $(function(){
            //mobile or tablet or desktop
            var client='<?php echo $layout; ?>'
            if(client=='desktop'){
                //do nothing
            }else if(client=='tablet'){
                $('#image-list img').each(function(){
                    $('this').attr('src',$(this).attr('src').replace('desktop','tablet'));  
                });
            }else{
                //mobile
                $('#image-list img').each(function(){
                    $('this').attr('src',$(this).attr('src').replace('desktop','mobile'));  
                });
            }
        });
    

            <div id="image-list">
            <img src="cdn/images/desktop/image1.jpg" width="500" height="200" alt="image1">
            <img src="cdn/images/desktop/image2.jpg" width="500" height="200" alt="image2">
            <img src="cdn/images/desktop/image3.jpg" width="500" height="200" alt="image3">
        </div>
    

    use jQuery


    sorry i didn’t know it don’t require lib.

        var client='<?php echo $layout; ?>'
        var list=document.getElementById('image-list');
        var img=list.getElementsByTagName('img');
        for(var i=0;i<img.length;i++){
            //img[i].src=img[i].src.replace('desktop',client);
            img[i].setAttribute('src',img[i].getAttribute('src-data').replace('desktop',client));
        }
    

    put it in the end of file.


    <img src-data="cdn/images/desktop/image1.jpg" width="500" height="200" alt="image1">
    

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

Sidebar

Related Questions

this is my first question I have searched for any launching activity questions and
This is my first question, so please be gentle. I'm trying out Apache Camel
First, this question first arose after working with Three.js where I tried/trying to build
First off this isn't for any homework question, it's just on a general type
this is my first question in here, and I would like to ask if
This is my first question on Stack Overflow, so I hope that I'm clear
this is my first question to SO so i'll try not to disgrace myself.
this is my first question here so be gentle ! recently i try to
this is my first question on stack overflow. Some quick background, this is not
This is my first question, but I've already found this site extremely helpful, so

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.