samedi 20 avril 2019

How do I convert an internal

I'm trying to convert an internal HTML script element into an external Javascript file. The element works perfectly in HTML, but I don't understand what I need to change in order for it to work from an external Javascript file. What I have right now "fires" - it does generate the random images - but it shows them full screen, blocking off the entire website.

Here is the working HTML


 <script class="randomad" type="text/javascript">
            var total_images = 7;
            var random_number = Math.floor((Math.random()*total_images));
            var random_img = new Array();
            random_img[0] = '<a href="http://www.antares-sellier.com/en/"><img src="javascript/js_images/antares.jpg"></a>';
            random_img[1] //....1-5 excluded here for brevity//
            random_img[6] = '<a href="http://www.scesports.org/"><img src="javascript/js_images/sces.jpg"></a>';
            document.write(random_img[random_number]);
            </script>

In the HTML file, I've replaced the script element with:

           <div id="firstads"></div>

and added

<script src="javascript/rec_images.js" async></script> 

to the head element.

In Javascript, I've removed the script tags and added a function, but I'm pretty new to Javascript and can't figure out what else I need to add or change to make the conversion work properly

Here is the malfunctioning Javascript code:

window.onload = random_ad;

 function random_ad () {
     document.getElementById("firstads");
    var total_images = 7;
    var random_number = Math.floor((Math.random()*total_images));
    var random_img = new Array();
    random_img[0] = '<a href="http://www.antares-sellier.com/en/"><img src="javascript/js_images/antares.jpg"></a>';
//....1-5 excluded here for brevity//
    random_img[6] = '<a href="http://www.scesports.org/"><img src="javascript/js_images/sces.jpg"></a>';
    document.write(random_img[random_number]);

 }

The goal is to have a hyperlinked image randomly generated in by an external javascript file. The images need to be small enough to fit in an aside column, but right now they are the size of the browser. I'd really appreciate any help with fixing this!




Aucun commentaire:

Enregistrer un commentaire