mardi 3 septembre 2019

Is it possible to use the return values of one if-statement as a condition in another?

I was wondering if it'd be possible to have the return value of one if-else used as a condition in another if-else.

In the first if-else, img in file2.js, there's one variable returned at any given point.

In the second if-else, link in file3.js, I'm attempting to use the output of img as part of link's conditions.

I've tried using = and ===, but that has proved to only load the first and last result of link's return values, respectively.

<!-- file1.html is only included for context and AFAIK, doesn't really affect my question: -->

<script async src="file2.js"></script>
<p>
    <a target="_blank" href="file3.html" id="aTarg" name="aTarg">
    <img src="https://i.imgur.com/4LtRreH.png" id="pic" name="pic"/>
        </a>
</p>
<p>....more article text</p>



/* file2.js */

const now = new Date();
 const timeNoon = now.getHours() === 12;

const CallingS1 = "https://i.imgur.com/5IaY11U.png";
const CallingS2 = "https://i.imgur.com/ANdRs50.png";

let img = function() {
    if (timeNoon) {
        return CallingS1;
    } else {
        return CallingS2;
    }
}
// function imgLink() works well:

function imgLink() {
    document.getElementById('pic').src=img();
}

imgLink();


<!-- file3.html: -->

<head>
<script src="file2.js"></script>
<script>
/* Here is where the problem's begin. I'm trying to use the output of img (in file2.js) be the conditions for link() */
  function link() { 
    if (img === CallingS1) {
        return location.replace("https://www.google.com/");
    } else if (img === CallingS2) {
        return location.replace("https://www.bing.com/");
    } else {
        return location.replace("https://stackoverflow.com/");
    }
  }
</script>
</head>
<body onload="link()">
</body>

I'm trying to have function link() rely on the outputs of let img = function().

Aucun commentaire:

Enregistrer un commentaire