www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Will only work when 'alert' is present

  1. #1
    Join Date
    Jun 2009
    Posts
    50

    Will only work when 'alert' is present

    Hello,

    I've got a perplexing problem to figure out. I currently have a an HTML form that is using an external JS file to load an XML, and then place the info into a dynamic table. Pretty straight forward.

    The entire application works flawlessly when everything is local. All files are located within the same directory. However, the only way I can get the JS file to read the XML file when the XML file is located within our 'web-hosting' environment (TeamSite) is if I place an 'alert' within the function that is reading the XML file.

    It will work fine if I do this. Talking with some other people, they suggested that placing that 'alert' within the function somehow makes the read enter a different security level. Does this make sense? If so does anyone know how to get around this?

    I have 3 XML files I'm pulling data from and would prefer not to have to 'alert' three times!


    Code:

    function initTraverse(file){//this function starts loading the xml document and finishes by looping through each branch
    loadXML(file)
    alert("Please answer every question!") THIS IS THE STATEMENT THAT MAKES IT WORK.
    var xmldoc=xmlDoc.documentElement
    traverse(xmldoc)//loop through the xml doc
    }


    function loadXML(xmlFile){//this function loads the xml document, if it is ready
    xmlFile.async="false"
    xmlDoc.onreadystatechange=verify
    xmlDoc.load(xmlFile)
    }


    function traverse(tree){
    try{
    for(i=0;i<xmlDoc.documentElement.childNodes.length;i++){
    var category = xmlDoc.documentElement.childNodes(i).childNodes(0).text
    var question = xmlDoc.documentElement.childNodes(i).childNodes(1).text
    var number = xmlDoc.documentElement.childNodes(i).childNodes(2).text
    category += number
    questionArray[questionArray.length++] = new addQuestion(category, question)//create new question within the questionArray
    }
    }
    catch(err){
    alert(err.description)
    }
    }

    Thanks!
    Last edited by hacket; 08-12-2009 at 10:35 AM.

  2. #2
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    I'm guessing that loadXML is an 'async' function. The 'alert' slows things down so the file can load before you fire traverse(xmldoc)
    Try moving thing around a little.... May solve the problem.
    Code:
    function initTraverse(file){
    	loadXML(file)
    }
    
    function loadXML(xmlFile){//this function loads the xml document, if it is ready
    	xmlFile.async="false"
    	xmlDoc.onreadystatechange=verify
    	xmlDoc.load(xmlFile)
    //	alert("Please answer every question!") THIS IS THE STATEMENT THAT MAKES IT WORK.
    	var xmldoc=xmlDoc.documentElement
    	traverse(xmldoc)//loop through the xml doc
    }

  3. #3
    Join Date
    Jun 2009
    Posts
    50
    Nope that didn't do anything.

    I forgot to post my verify() function

    function verify(){//this function verifies that the xml doc is ready to be worked with
    if(xmlDoc.readyState!=4)
    {
    return false
    }
    }

  4. #4
    Join Date
    Jun 2009
    Posts
    50
    Your suggestion of slowing the code down seems to work though. I put in a loop that would take a long time to go through and it got through and did what I wanted.

    But when I use this code it doesn't work. I'm not sure why. I'll keep playing but if someone sees the problem please post?


    function loadXML(xmlFile){//this function loads the xml document, if it is ready

    xmlFile.async="false"
    xmlDoc.onreadystatechange=verify
    xmlDoc.load(xmlFile)
    setTimeout("startTrav()", 10000)
    }
    function startTrav(){
    alert('hello')
    var xmldoc=xmlDoc.documentElement
    traverse(xmldoc)



    }

  5. #5
    Join Date
    Jun 2009
    Posts
    50
    Actually I take back that previous post. It's not because it's slowing it down that it's working. It's because it slows it down enough to cause an alert saying that a script is running slowly. Letting the script run makes it read the XML file.

    But speeding up the loop by just enough to bypass the alert makes it NOT read the XML file.

    So once again it's the alert that's making it read the XML file....

  6. #6
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    Quote Originally Posted by hacket View Post
    I forgot to post my verify() function
    Same thinking, but..
    Code:
    function initTraverse(file){
    	loadXML(file)
    }
    
    function loadXML(xmlFile){//this function loads the xml document, if it is ready
    	xmlFile.async="false"
    	xmlDoc.onreadystatechange=verify
    	xmlDoc.load(xmlFile)
    }
    
    function verify(){//this function verifies that the xml doc is ready to be worked with
    	if(xmlDoc.readyState==4) {
    	//	alert("Please answer every question!") THIS IS THE STATEMENT THAT MAKES IT WORK.
    		var xmldoc=xmlDoc.documentElement
    		traverse(xmldoc)//loop through the xml doc
    	}
    	else return false;
    }

  7. #7
    Join Date
    Jun 2009
    Posts
    50
    Well that half worked.

    I only have to have one 'alert' now. I have my functions that traverse everything working. But get this. It's really weird. To demonstrate the problem a little more clearly:

    I have my three functions that starting off the traversing of each XML file in a row. They each call subsequent functions and such.

    Then right after these three main functions I have two statements doing the exact same thing. Alerting me of what is in the first position of the array I'm putting the XML data in.

    The first statement returns....NULL...which means there's nothing in the array. Nothing gets displayed on screen from the XML file.

    The second statement returns....[object]...which means the data is in there. Everything is then displayed correctly.


    Nothing happens between these two statements! I don't get it.

  8. #8
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    This looks a lot like an Ajax call.

    If you are calling 3 times, it's possible that the returns are getting scrambled somehow.
    What I would suggest, if you are not already doing it, is to call the next 'initTraverse(file)' function from the previous 'verify' function.
    That way each must complete before calling the next.

  9. #9
    Join Date
    Jun 2009
    Posts
    50
    I'm not really following your logic when you say that. If I want each to complete before beginning the next why would I call during the "verify" of the previous?

    The fact that it displays what I want merely after calling an alert suggests that it's reading the data just fine. I don't understand why the alert would make it work though...

    I'll just post all my code for this, I think this is all of it

    Code:

    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
    var fieldsXmlDoc = new ActiveXObject("Microsoft.XMLDOM")
    var xmlStaticInfoDoc = new ActiveXObject('Microsoft.XMLDOM')
    var questionArray = new Array()
    var fieldsArray = new Array()
    var surveyTitle = ""

    initTraverse("http://........./AssessmentQuestions.xml")//open questions xml document
    initTraverseFields("http://........../AssessmentFields.xml")//open fields xml document
    initTraverseStaticInfo("http://................/AssessmentStaticInfo.xml")//open static info xml document

    alert(questionArray[0])
    alert(questionArray[0]) These are the lines that will display "null" and [object] respectively

    //xml initial functions
    ////////////////////////////////////////////////////////////////////
    function initTraverse(file){//this function starts loading the xml document and finishes by looping through each branch
    loadXML(file)
    }
    function initTraverseFields(travfile){
    loadFieldXML(travfile)
    }
    function initTraverseStaticInfo(travfileStaticInfo){
    loadStaticInfoXML(travfileStaticInfo)
    }

    //xml load functions
    /////////////////////////////////////////////////////////////////////////
    function loadXML(xmlFile){//this function loads the xml document, if it is ready
    xmlFile.async="false"
    xmlDoc.onreadystatechange=verify
    xmlDoc.load(xmlFile)
    }

    function loadFieldXML(xmlFile){//this function loads the xml document, if it is ready
    xmlFile.async="false"
    fieldsXmlDoc.onreadystatechange=verifyFieldXML
    fieldsXmlDoc.load(xmlFile)
    }
    function loadStaticInfoXML(xmlFile){
    xmlFile.async="false"
    xmlStaticInfoDoc.onreadystatechange=verifyStaticInfoXML
    xmlStaticInfoDoc.load(xmlFile)
    }

    //xml verify functions
    ////////////////////////////////////////////////////////////////////////
    function verify(){//this function verifies that the xml doc is ready to be worked with
    if(xmlDoc.readyState==4)
    {
    var xmldoc=xmlDoc.documentElement
    traverse(xmldoc)

    }else{
    return false}
    }
    function verifyFieldXML(){//this function verifies that the xml doc is ready to be worked with
    if(fieldsXmlDoc.readyState==4)
    {
    var xmldoctrav=fieldsXmlDoc.documentElement
    traverseFields(xmldoctrav)//loop through the xml doc
    }else{
    return false}
    }
    function verifyStaticInfoXML(){
    if(xmlStaticInfoDoc.readyState==4)
    {
    var xmldoctravStatic=xmlStaticInfoDoc.documentElement
    traverseStaticInfo(xmldoctravStatic)//loop through the xml doc
    }else{
    return false}
    }

    //xml traverse functions
    //////////////////////////////////////////////////////////////////////
    //this function traverses the 'fields' xml document and put the name and label of each field into the array 'fieldsArray'
    function traverseFields(tree){
    try{
    for(i=0;i<fieldsXmlDoc.documentElement.childNodes.length;i++){
    var nameOfField = fieldsXmlDoc.documentElement.childNodes(i).childNodes(0).text
    var labelOfField = fieldsXmlDoc.documentElement.childNodes(i).childNodes(1).text
    fieldsArray[fieldsArray.length++] = new addField(nameOfField, labelOfField)
    }
    }
    catch(err){
    alert(err.description)
    }

    }
    function traverseStaticInfo(tree){
    try{
    for(i=0;i<xmlStaticInfoDoc.documentElement.childNodes.length;i++){
    if(xmlStaticInfoDoc.documentElement.childNodes[i].tagName == "surveyTitle"){
    surveyTitle = xmlStaticInfoDoc.documentElement.childNodes[i].text
    }
    }
    }
    catch(err){
    alert(err.description)
    }

    }

    //this function traverse the XML document and puts the values into the array 'questionArray'
    function traverse(tree){
    try{
    //alert(xmlDoc.documentElement)
    //alert(xmlDoc.documentElement)
    for(i=0;i<xmlDoc.documentElement.childNodes.length;i++){
    var category = xmlDoc.documentElement.childNodes(i).childNodes(0).text
    var question = xmlDoc.documentElement.childNodes(i).childNodes(1).text
    var number = xmlDoc.documentElement.childNodes(i).childNodes(2).text
    category += number
    questionArray[questionArray.length++] = new addQuestion(category, question)//create new question within the questionArray
    }
    }
    catch(err){
    alert(err.description)
    }
    }
    Last edited by hacket; 08-12-2009 at 05:41 PM.

  10. #10
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    Remember, this info may not be spot on. It's a somewhat educated guess.

    initTraverse("http://........./AssessmentQuestions.xml")//open questions xml document
    initTraverseFields("http://........../AssessmentFields.xml")//open fields xml document
    initTraverseStaticInfo("http://................/AssessmentStaticInfo.xml")//open static info xml document

    This will trigger 3 async loadXML calls, one immediatly after the other. (not waitng for a return)
    and the returns will all come at different times depending on who knows what and can easily get scrambled.

    What I am suggesting is to call 'initTraverseFields()' after 'initTraverse()' has completed. (same for 'initTraverseStaticInfo()' and 'initTraverseFields()')
    Like this....
    Code:
    function verifyFieldXML(){//this function verifies that the xml doc is ready to be worked with
    	if(fieldsXmlDoc.readyState==4) {
    		var xmldoctrav=fieldsXmlDoc.documentElement
    		traverseFields(xmldoctrav)//loop through the xml doc
    		initTraverseFields("http://........../AssessmentFields.xml")
    	}
    	else {
    		return false
    	}
    }

  11. #11
    Join Date
    Jun 2009
    Posts
    50
    But isn't that calling each of the init functions 'within' the first one, etc, etc....? It doesn't seem to be waiting for each to finish. The way it is now each is called in succession and I'm assuming this completes each. Would each need to return a value is that what you mean? Maybe I'm not understanding what you mean quite yet...

    Also what exactly do you mean by 'async' function?

  12. #12
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    Quote Originally Posted by hacket View Post
    But isn't that calling each of the init functions 'within' the first one, etc, etc....? It doesn't seem to be waiting for each to finish.
    Well kind of.
    After the first init is called, we need to wait for the 'callback'. Now we complete the callback action in the 'traverseFields(xmldoctrav)'. Once that has completed, we call the next 'init...()'.

    Quote Originally Posted by hacket View Post
    The way it is now each is called in succession and I'm assuming this completes each.
    Yes and no! They are called in succession, but each may or may not complete before the next is called.

    When I say asynch I am meaning that you call one of the 'init..()' functions and then, after an unknown period, you get a return in the applicable verify() function. (know as 'callback'). However, the javascript does not wait for the callback and will immediately go on the the next program step. In this case, the next 'init...()'

    Hope that makes sense.

  13. #13
    Join Date
    Jun 2009
    Posts
    50
    So bascially the way I'm understanding what you mean is that the javascript will call the first 'init' function, and then as it's going through the code in this first function, it will call the second 'init' function, before the first has completed?

    This doesn't make sense. I've never heard of that. It defies programming logic! Lol.

    Anyway I tried that, and it didn't work. I move the second and third 'init' functions into the first and second 'verify' functions, and it actually didn't work at all, even when I had the alerts in there.

    Sigh....this is frustrating.

  14. #14
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    Quote Originally Posted by hacket View Post
    So bascially the way I'm understanding what you mean is that the javascript will call the first 'init' function, and then as it's going through the code in this first function, it will call the second 'init' function, before the first has completed?
    That's exactly what your current code is doing. What I am suggesting will avoid that.
    edit again
    That's the problem with asynch functions. The 'load' function goes to get the file data, but that could take a while. (relatively). Meanwhile javascript presses on to the next function. Then a some undetermined time, the data come back and is processed by the 'transfer' function aschchronously.

    Here's how I would do it.
    I re-ordered things a little and tested with a simple .xml and some alerts

    Maybe this will clarify what I mean.

    Code:
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
    var fieldsXmlDoc = new ActiveXObject("Microsoft.XMLDOM")
    var xmlStaticInfoDoc = new ActiveXObject('Microsoft.XMLDOM')
    var questionArray = new Array()
    var fieldsArray = new Array()
    var surveyTitle = ""
    
    
    initTraverse("http://........./AssessmentQuestions.xml")//open questions xml document
    function initTraverse(file){ //this function starts loading the xml document and finishes by looping through each branch
    	loadXML(file)
    }
    function loadXML(xmlFile){//this function loads the xml document, if it is ready
    	xmlFile.async="false"
    	xmlDoc.onreadystatechange = function() {
    		if(xmlDoc.readyState==4) {
    			var xmldoc=xmlDoc.documentElement
    			traverse(xmldoc)
    			initTraverseFields("http://........../AssessmentFields.xml")//open fields xml document
    		}
    		else return false;
    	}
    	xmlDoc.load(xmlFile)
    }
    function traverse(tree){	//this function traverse the XML document and puts the values into the array 'questionArray'
    	for(i=0; i<xmlDoc.documentElement.childNodes.length; i++){
    		var category = xmlDoc.documentElement.childNodes(i).childNodes(0).text
    		var question = xmlDoc.documentElement.childNodes(i).childNodes(1).text
    		var number = xmlDoc.documentElement.childNodes(i).childNodes(2).text
    		category += number
    		questionArray[questionArray.length++] = new addQuestion(category, question)//create new question within the questionArray
    	}
    }
    
    
    function initTraverseFields(travfile){
    	loadFieldXML(travfile)
    }
    function loadFieldXML(xmlFile){//this function loads the xml document, if it is ready
    	xmlFile.async="false"
    	fieldsXmlDoc.onreadystatechange = function() {
    		if(fieldsXmlDoc.readyState==4) {
    			var xmldoctrav=fieldsXmlDoc.documentElement
    			traverseFields(xmldoctrav)
    			initTraverseStaticInfo("http://................/AssessmentStaticInfo.xml")//open static info xml document
    		}
    		else return false;
    	}
    	fieldsXmlDoc.load(xmlFile)
    }
    function traverseFields(tree){
    	for(i=0; i<fieldsXmlDoc.documentElement.childNodes.length; i++) {
    		var nameOfField = fieldsXmlDoc.documentElement.childNodes(i).childNodes(0).text
    		var labelOfField = fieldsXmlDoc.documentElement.childNodes(i).childNodes(1).text
    		fieldsArray[fieldsArray.length++] = new addField(nameOfField, labelOfField)
    	}
    }
    
    
    function initTraverseStaticInfo(travfileStaticInfo){
    	loadStaticInfoXML(travfileStaticInfo)
    }
    function loadStaticInfoXML(xmlFile){
    	xmlFile.async="false"
    	xmlStaticInfoDoc.onreadystatechange = function() {
    		if(xmlStaticInfoDoc.readyState==4) {
    			var xmldoctravStatic=xmlStaticInfoDoc.documentElement
    			traverseStaticInfo(xmldoctravStatic)
    		}
    		else return false
    	}
    	xmlStaticInfoDoc.load(xmlFile)
    }
    function traverseStaticInfo(tree) {
    	for(i=0; i<xmlStaticInfoDoc.documentElement.childNodes.length; i++){
    		if(xmlStaticInfoDoc.documentElement.childNodes[i].tagName == "surveyTitle"){
    			surveyTitle = xmlStaticInfoDoc.documentElement.childNodes[i].text
    		}
    	}
    }
    Last edited by Nedals; 08-12-2009 at 07:30 PM.

  15. #15
    Join Date
    Jun 2009
    Posts
    50
    Ok well I understand that now and used that model and got it to work, but it's working the same as before.

    Two alerts, the first displaying 'null' and the second displaying the data. It still doesn't work without an alert statement.

    Like I said before, the fact that the two alerts display 'null' and then the data...with no statements in between...suggests that the alert statement is doing something to the data in the array itself to make it 'visible'. I've never encountered anything like this before.

    Thank you for your help btw
    Last edited by hacket; 08-12-2009 at 07:36 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles