Jump to content

Help Please -- This Is Weird


turtle

Recommended Posts

I am trying to write a script in (mainly) Javascript to respond to the rating of a site on siteadvisor. Anyway. here's the code:

<html><head><title>SiteAdvisor Bookmarklet for Chrome</title></head><body><base href="http://siteadvisor.com/sites/" src="http://siteadvisor.com/sites/"><!-- include SA review page --><?php $URL=$_GET["url"]; include("http://siteadvisor.com/sites/$URL"); ?> <!-- begin code --><script>function getRating(){if(document.getElementById("siteVerdict").class="siteGreen"){rating="green";}if(document.getElementById("siteVerdict").class="siteYellow"){rating="yellow"}if(document.getElementById("siteVerdict").class="siteRed"){rating="red"}return rating;}alert(getRating());</script></body></html>

Oh, this uses PHP code to get the contents of the page.The siteVerdict div's class says what the rating is....

		<div id="siteVerdict"  class="siteGreen"> <------			<img src="/images/green-xbg2.gif" alt="Green Verdict Image" border="0" />			<p class="intro">We tested this site and didn't find any significant problems.</p>			<p class="finePrint">Are you the owner of this site?						<a href='http://user.siteadvisor.com/forums/websiteOwnerVerification.php?domain=google.com'>Leave a comment</a></p>		</div><!-- /siteVerdict -->

...Anyway, the problem is that the JS always responds saying it is red, even if it is green or yellow. Help please.

Link to comment
Share on other sites

1. You're using the assignment operator ( = ) instead of the comparison operator (==). That means that every if statement is returning TRUE, and the final one determines the value of rating.2. The property you should be testing for is className, not class.

Link to comment
Share on other sites

Thanks. It works.But why do you think it outputted red? It seems like it would output nothing.Code:

<html><head><title>SiteAdvisor Bookmarklet for Chrome</title></head><body><base href="http://siteadvisor.com/sites/" src="http://siteadvisor.com/sites/"><!-- include SA review page --><?php $URL=$_GET["url"]; include("http://siteadvisor.com/sites/$URL"); ?><!-- begin code --><script>function getRating(){if(document.getElementById("siteVerdict").className=="siteGreen"){rating="green";}if(document.getElementById("siteVerdict").className=="siteYellow"){rating="yellow"}if(document.getElementById("siteVerdict").className=="siteRed"){rating="red"}return rating;}alert(getRating());</script></body></html>

Link to comment
Share on other sites

It's easier if I simplify it. Basically, you started with an assignment statement embedded in an if statement. Kind of like this:if (x = 1) {//do something}Since x = 1 is an assignment, the if statement is simply evaluating whether the assignment happens. An assignment can break. Like if you said, 5 = 7. You cannot assign the value of 7 to the number 5. Doing that would actually terminate the function. But x = 1 succeeds, because it's a valid thing to do. When you wrap that success in an if statement, it evaluates as TRUE, and executes the code in the braces.Since all three of your original statements worked the same way, ALL of them were evaluating as true. In each case, you were creating a new property for "siteVerdict" called "class" (it is legal to create new properties in that way) and then assigning that property the values "siteGreen", "siteYellow", and "siteRed". Each assignment worked, so each if test evaluated as true, and executed its code, which assigned an appropriate value to rating. rating actually had all three values at one time. It returned as "red" because that was the last one. It was not an arbitrary choice. Whichever assignment came last would have been the one that stuck.If you had used the if . . . else construction, then the first value would have returned, since that statement also evaluated as true.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...