Solved

Cannot process .load result correctly to change and existing set variable

Posted on 2013-02-02
9
218 Views
Last Modified: 2013-02-03
Hello!

I'm trying to write some code that verifies and email address, returning Y if valid and N if not - this bit works fine, with the ValidEmail div id having the right result returned - i.e. either Y or N. However, I cannot get the valid_flaf indicator set correctly after the load event?

The alert of valid_flag always returns 0, irrespective if ValidEmail').html()  is Y or N

-------------------------------

valid_flag=9;

$('#ValidEmail').load('/Validate_EMAIL.asp?VerifyEmail=' + document.TestForm.AdminEmail.value,function(){

      if ($('#ValidEmail').html() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }});

alert(valid_flag);

-------------------------------


Kind Regards

JamWales
0
Comment
Question by:Jamie
  • 5
  • 4
9 Comments
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
I would say because the html for the ValidEmail div never equals "Y".

Try:

if ($('#ValidEmail').text() == "Y") {

. . .
0
 

Author Comment

by:Jamie
Comment Utility
Hi tommyBoy,

Thanks for the above.

It seems to work, in that the valid_flag now seems to be set correctly - although the alert(valid_flag); still shows 0, even when it appears to be set to 1 - but my calculation further down now works...

... however, if i remove the alert(valid_flag); statement, the valid_flag is not set correctly?!

Kind Regards

JamWales
0
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
You have code in addition to what was posted here and that is affecting the outcome. I say that because this simple test works.
<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function(){
      var valid_flag=9;
  
      if ($('#ValidEmail').text() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }//}); 

      alert(valid_flag);
  });
</script>
</head>
<body>
    <div id="ValidEmail">Y</div>
</body>
</html>

Open in new window

0
 

Author Comment

by:Jamie
Comment Utility
Hi tommyBoy,

Yes, as you say, your script works fine - but the problem is as originally described, with the .load command. I'm not even sure what I'm trying to do is possible?

Basically, the response from the Validate_EMAIL.asp script is placed into the <div id="ValidEmail"></div> - which is then used to determine the value of valid_flag by function().

Kind Regards

JamWales
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Jamie
Comment Utility
Hi tommyBoy,

I've found the cause of my problem, but not the solution!

I've amended your script below to show the issue. In my script, the variable valid_flag is set outside the .load function, so the change is not being reflected - hence why the alert(valid_flag); always returns 0 - instead of 1 for Y and 0 for N.

The below alert box will incorrectly display 9 instead of 1, as Y in ValidEmail in div tag.

Any ideas on how to resolve this?

Kind Regards

JamWales

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

   var valid_flag=9;

  $(document).ready(function(){
   
 
      if ($('#ValidEmail').text() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }//});

     
  });
 
  alert(valid_flag);
 
</script>
</head>
<body>
    <div id="ValidEmail">Y</div>
</body>
0
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
Any script in the head section that's not in a function is executed before the window.onload event or in this case the jquery equivalent, $(document).ready().

So...the alert is executing before the code inside the ready() function. Variable valid_flag equals 9 at that point.
0
 

Author Comment

by:Jamie
Comment Utility
Hi tommyBoy,

Thanks for your reply, I understand what you are saying, but my code below is placed into an existing function, so is not called until the function is called by the user pressing a text link on the webpage - this calls; javascript:total_cost()

From what you say and from what happens, it appears the .load function is being run local to itself and it's results not available to the function it resides it.

So not sure if it is the construct of my .load is still the problem?

Kind Regards

JamWales


valid_flag=9;

$('#ValidEmail').load('/Validate_EMAIL.asp?VerifyEmail=' + document.TestForm.AdminEmail.value,function(){

      if ($('#ValidEmail').html() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }});

alert(valid_flag);
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
Comment Utility
The .load function itself and the callback run asynchronously to the other code outside of it. So, because you have the alert outside of the callback function it is executing before the callback when valid_flag still equals 9.

If you code it as follows, even though the global valid_flag variable is instantiated outside the .load function, the valid_flag value will be re-assigned inside the .load callback and you would see the correct result in the alert.

var valid_flag=9;

$('#ValidEmail').load('/Validate_EMAIL.asp?VerifyEmail=' + document.TestForm.AdminEmail.value,function(){

      if ($('#ValidEmail').html() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }      
      alert(valid_flag);
});

This assumes of course that your Validate_EMAIL.asp file returns exactly a "Y" or an "N" only with no whitespace, other text or html. Otherwise, you may need to do something like this in your conditional:

if ($('#ValidEmail').text().indexOf("Y") >= 0) {
0
 

Author Closing Comment

by:Jamie
Comment Utility
Explained the solution - and me realising what I'm trying to do in my script isn't possible.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now