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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:00:48+00:00 2026-06-17T05:00:48+00:00

I’m trying to load a variable from a file using javascript. I’ve found some

  • 0

I’m trying to load a variable from a file using javascript. I’ve found some examples but I can’t seem to make it work and could really use some help on getting my syntax right.

Basically, I want to load a random ad image on a page, but I would like the list of ads to be pulled from a file. Currently I’m loading the images using the following script which I found on the internet:

    <script type="text/javascript">
     var picPaths = [
      '/images/ad-1.jpg',
      '/images/ad-2.jpg',
      '/images/ad-3.jpg',
      '/images/ad-4.jpg'
     ]
     var oPics = [];
     for(i=0; i < picPaths.length; i++){
      oPics[i] = new Image();
      oPics[i].src = picPaths[i];
     }
     curPic = Math.floor(Math.random()*oPics.length);
     window.onload=function(){
      document.getElementById('imgRotator').src = oPics[curPic].src;
     }
    </script>

I have been trying to get the picPath variable value to load from a file (instead of stating it in the code). I found some code here on stackoverflow and tried adjusted it to the following:

    var picPaths = new XMLHttpRequest();
    picPaths.open('GET', '/images/liveimages.inc');
    picPaths.send();

I also created the file /images/liveimages.inc which containts the following:

     '/images/ad-1.jpg',
     '/images/ad-2.jpg',
     '/images/ad-3.jpg',
     '/images/ad-4.jpg'

But, alas, it’s not working and I’m not programmer enough to fix it. 🙁 I’m thinking my syntax is off but my code could be off too since I am not a JavaScript guy.

Any help would be appreciated and thanks for taking the time to read (and respond) to my question! 😀

  • 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-17T05:00:50+00:00Added an answer on June 17, 2026 at 5:00 am

    If you store the data file as JSON you can use AJAX/XMLHTTPRequest to fetch it, and JSON.parse (available in all modern browsers) to read it.


    An easier way perhaps is just to have a script that contains just the data, like:

    var picPaths = [
     '/images/ad-1.jpg',
     '/images/ad-2.jpg',
     '/images/ad-3.jpg',
     '/images/ad-4.jpg'
    ];
    

    And then include your scripts in the correct order:

    <script type="text/javascript" src="picpaths.js"></script>
    <script type="text/javascript" src="ad_script.js"></script>
    

    ad_script.js will be able to access picPaths.

    You could have some server-side script generate picpaths.js for you, for instance by looking at the contents of a folder or a database and pulling the ad info from that.

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

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am reading a book about Javascript and jQuery and using one of the

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.