Hot-Honeys



.

Author Topic: How can this second alert be viewed?  (Read 196 times)

0 Members and 1 Guest are viewing this topic.

Offline SenileDog

  • Moderator
  • *****
  • Joined: Apr 2016
  • Location: US West Coast
  • Posts: 586
  • Images: 653
  • Reputation: 25
  • Gender: Male
How can this second alert be viewed?
« on: November 20, 2016, 04:19 pm »
this is a snippet from a CDATA function:

Code: [Select]
for ( var i = 0; i < 3; i++) {
if (i = 0){alert("Hello IHG!")"}
if (i = 1){alert("You will never see me!")}
return iUrl ? {imgUrl: iUrl, status: "OK"} : {imgUrl: null, status: "REQUEUE"}
}

Offline firewaran

  • Moderator
  • *****
  • Joined: Apr 2016
  • Posts: 264
  • Reputation: 18
  • Gender: Male
Re: How can this second alert be viewed?
« Reply #1 on: November 20, 2016, 09:22 pm »
I think in javascript one equal (=) means assign, only two equals (==) mean compare.

so the following should work and show you the second alert.

Code: [Select]
for ( var i = 0; i < 3; i++) {
if (i == 0){alert("Hello IHG!")"}
if (i == 1){alert("You will never see me!")}
return iUrl ? {imgUrl: iUrl, status: "OK"} : {imgUrl: null, status: "REQUEUE"}
}

Offline Rookie

  • Newbie
  • *
  • Joined: May 2016
  • Location: On the other side of the TunnelBear...
  • Posts: 12
  • Reputation: 5
Re: How can this second alert be viewed?
« Reply #2 on: November 20, 2016, 11:08 pm »
I have two comments:
- {alert("Hello IHG!")"} is badly written
- the statement i++ is never reached (the return statement interrupts the for loop immediately)

Offline SenileDog

  • Moderator
  • *****
  • Joined: Apr 2016
  • Location: US West Coast
  • Posts: 586
  • Images: 653
  • Reputation: 25
  • Gender: Male
Re: How can this second alert be viewed?
« Reply #3 on: November 21, 2016, 12:02 am »
to firewaran: Thank you, you are perfectly right it is if (i == 0) otherwise the affectation of i = 0 would cause an infinite loop on i = 1.

to Rookie: yes it is badly written; correct one is: if (i == 0) alert("Hello IHG!");

and yes it is never reached.

But is there a way to do sort of a try and catch or a loop

for example

iUrl can be the following:

http:\\foo.com\main\image\a.jpg

http:\\foo.com\server\image\a.jpg

http:\\foo.com\repository\image\a.jpg

how can we try the first one, possibly fail, then try the second one

Is there a way to detect the first one failed?

JFYI I cannot use a match an do (server|main|repository) in a regex.

that what I was very awkwardly trying in a for loop

for ( var i = 0; i < 3; i++) {
   if (i == 0) server="main;
   if (i == 1) server="server;
   if (i == 2){server="repository";
   return iUrl ? {imgUrl: server + iUrl, status: "OK"} : {imgUrl: null, status: "REQUEUE"}   
   }   

Thanks Guys, SD

Offline firewaran

  • Moderator
  • *****
  • Joined: Apr 2016
  • Posts: 264
  • Reputation: 18
  • Gender: Male
Re: How can this second alert be viewed?
« Reply #4 on: November 21, 2016, 12:17 pm »
As rookie said, when the "return" is reached the functions ends, no matter if you are in a loop or not.
So you would need a condition for the perform

My first idea would be to make a XmlHttpRequest(), if you get something like "404 page not found" or something you know you have to try the next "location".

Maybe something like:
Code: [Select]
var req = new XMLHttpRequest();
for ( var i = 0; i < 3; i++) {
   if (i == 0) server="main";
   if (i == 1) server="server";
   if (i == 2){server="repository";

   req.open("POST", server + iUrl, false);
   req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   req.send();
   
   if (req.status != "404") {
      return {imgUrl: server + iUrl, status: "OK"}
   }       
}   
return {imgUrl: null, status: "ABORT"}

When you requeue it then the function will just start over and not remember that is was already done one time.

As far as i understood the requeue, you can split up the solution into two parts when it is to complicated in one (for example)
IHG will just run again the next solutions fitting the regex for the "new url".

Offline SenileDog

  • Moderator
  • *****
  • Joined: Apr 2016
  • Location: US West Coast
  • Posts: 586
  • Images: 653
  • Reputation: 25
  • Gender: Male
Re: How can this second alert be viewed?
« Reply #5 on: November 22, 2016, 01:53 am »
Thank you Fire. I did not think about the 404, silly me.  :D

Cheers, SD