简体   繁体   English

如何在使用JavaScript从外部文件加载广告列表的页面上旋转广告?

[英]How do I rotate an ad on a page loading the list of ads from an external file using JavaScript?

I'm trying to load a variable from a file using javascript. 我正在尝试使用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: 目前,我正在使用在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). 我一直在尝试从文件中加载picPath变量值(而不是在代码中说明)。 I found some code here on stackoverflow and tried adjusted it to the following: 我在这里在stackoverflow上找到了一些代码,并尝试将其调整为以下代码:

    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/liveimages.inc,其中包含以下内容:

     '/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. 但是,a,它无法正常工作,而且我还没有足够的程序员来修复它。 :-( I'm thinking my syntax is off but my code could be off too since I am not a JavaScript guy. :-(我以为我的语法已经关闭,但是我不是JavaScript专家,我的代码也可能关闭了。

Any help would be appreciated and thanks for taking the time to read (and respond) to my question! 任何帮助将不胜感激,并感谢您抽出时间阅读(并回复)我的问题! :-D :-D

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. 如果将数据文件存储为JSON,则可以使用AJAX / XMLHTTPRequest来获取它,并使用JSON.parse (在所有现代浏览器中都可用)读取它。


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 . ad_script.js将能够访问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. 您可能需要一些服务器端脚本来为您生成picpaths.js,例如,通过查看文件夹或数据库的内容并从中获取广告信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM