Jump to content

tinfanide

Members
  • Posts

    445
  • Joined

  • Last visited

Posts posted by tinfanide

  1. is there a reason you can't convert the string into JSON and then access it as an array? some browsers support it natively, if not Douglas Crockford has a library.https://github.com/d...ockford/JSON-js
    var string  = "[{"Title": "Book4","Barcode": "bk4","Location": "Lib4"},{"Title": "Book4","Barcode": "bk4","Location": "Lib1"}]";var json = JSON.parse(string);console.log(json[0]);

    Yes, thanks for your codes.I was doing JSON.parse but after success, I just thought that whether it would be faster if the json data is parsed into a string.As I need to loop through the json array (stored in a .JSON file) for searching purposes through AJAX, that's why I want to test if a JSON string is faster than looping through an array.
  2. Otherwise, the pattern would be to match a "{" character, then any number of characters that are not "}", then "}".
    Ya just inspired me. Yes, ya're very right.
    // IE7+ returns:// FF9 returns:found = jsonData.match(/{[^{]*Lib4[^}]*}/i);

  3. "[{"Title": "Book4","Barcode": "bk4","Location": "Lib4"},{"Title": "Book4","Barcode": "bk4","Location": "Lib1"}]"
    I want to extract
    {"Title": "Book4","Barcode": "bk4","Location": "Lib4"}
    Have tried:
    str.match(/[\W]*Lib4[\W]*/);

    but return nothing. How can I take it out with JavaScript Regular Expression?

  4. display: table-row will work on any element. The point of the display property is to make an element behave like something it is not.The <tr> element doesn't need "display: table-row" because it already is a table row.
    But it really doesn't work in IE7,
    <style>.form_style{display: table-cell;width: 250px; height: 40px;}</style></head><body><form name="queries"><div style="display: table;"><div style="display: table-row;"><div class="form_style"><input type="checkbox" name="libraries" value="Lib1" />Lib1<input type="checkbox" name="libraries" value="Lib2" />Lib2<input type="checkbox" name="libraries" value="Lib3" />Lib3<input type="checkbox" name="libraries" value="Lib4" />Lib4<br /><input type="checkbox" name="items" value="Title" />Title<input type="checkbox" name="items" value="Barcode" />Barcode</div><div class="form_style"><input type="input" name="query" value="" /></div></div></div></form>

  5. If I'm remembering correctly, for the table-cell to display properly, it needs to be wrapped in an element with table-row display which is wrapped in another element with table display.
    <form name="queries"><table style="display: table; height: 50px;">    <tr style="display: table-row;">	    <td style="display: table-cell; width: 250px;">	    <input type="checkbox" name="libraries" value="Lib1" />Lib1	    <input type="checkbox" name="libraries" value="Lib2" />Lib2	    <input type="checkbox" name="libraries" value="Lib3" />Lib3	    <input type="checkbox" name="libraries" value="Lib4" />Lib4	    <br />	    <input type="checkbox" name="items" value="title" />Title	    <input type="checkbox" name="items" value="barcode" />Barcode	    </td>	    <td style="display:table-cell; width: 100px;">	    <input type="input" name="query" value="" />	    </td>    </tr></table></form>

    Yes, you are right. But display: table-row only goes with <table>, not <div>.

  6. <div><form name="queries" style="display: table;"><div style="display: table-cell; width: 250px; height: 200px;"><input type="checkbox" name="libraries" value="Lib1" />Lib1<input type="checkbox" name="libraries" value="Lib2" />Lib2<input type="checkbox" name="libraries" value="Lib3" />Lib3<input type="checkbox" name="libraries" value="Lib4" />Lib4<br /><input type="checkbox" name="items" value="title" />Title<input type="checkbox" name="items" value="barcode" />Barcode</div><div style="display:table-cell; width: 100px; height: 200px;"><input type="input" name="query" value="" /></div></form></div>

    Any IE8- (in my case IE7) fix for not displaying the table-cells properly?

  7. declare it outside of the function scope. i.e.
    <script>var oJSON, sJSON;..

    however, if you plan on using the value of sJSON across two asynchronous requests (with the second being dependent on the first), you would probably want to make the second AJAX request as part of the callback function of the first. (at that point it won't need to be global though).

    How's a callback function like?Is it to check the readyState and status of the first AJAX request before going to the second one?
  8. var ajax = function(query){xmlhttp = (window.XMLHttpRequest) ? xmlhttp=new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;xmlhttp.onreadystatechange = function(){   jsonData = JSON.parse(xmlhttp.responseText);   (xmlhttp.readyState==4 && xmlhttp.status==200) ?   func(query) : null ;   }xmlhttp.open("GET","lib.json",true);xmlhttp.send();} function func(query){ // fail to check an undefined AJAX responseText if(typeof(xmlhttp.responseText)=="undefined" && xmlhttp.responseText != null){  alert(0);  }else {  document.getElementById("result").innerHTML = jsonData[1][query];  }}

    This is the testing page:http://lifelearning....st.com/lib.html Just wanna ask how to check an undefined AJAX responseText.Many thanks.

    Solve it.It should be
    if (typeof(jsonData[1][query])!=="undefined" && (jsonData[1][query])!=null){ // something}

    not jsonData or xmlhttp.responseText that should not be null or "undefined"

  9. var ajax = function(query){xmlhttp = (window.XMLHttpRequest) ? xmlhttp=new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;xmlhttp.onreadystatechange = function(){   jsonData = JSON.parse(xmlhttp.responseText);   (xmlhttp.readyState==4 && xmlhttp.status==200) ?   func(query) : null ;   }xmlhttp.open("GET","lib.json",true);xmlhttp.send();} function func(query){ // fail to check an undefined AJAX responseTextif(typeof(xmlhttp.responseText)=="undefined" && xmlhttp.responseText != null){  alert(0);  }else {  document.getElementById("result").innerHTML = jsonData[1][query];  }}

    This is the testing page:http://lifelearning.xtreemhost.com/lib.html Just wanna ask how to check an undefined AJAX responseText.Many thanks.

  10. <html><!--php_json.php--><head><script src="json2.js"></script><script src="php.js"></script><script>ajax("php_json.json",function(){oJSON = JSON.parse(xmlhttp.responseText);sJSON = JSON.stringify(oJSON);/*put it hereokay*/});// here, the variable sJSON is undefinedajax("php.php?json="+sJSON,function(){  alert(xmlhttp.responseText);   });//</script></head><body></body></html>

    // php.jsfunction ajax(source,func){xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;xmlhttp.onreadystatechange = function(){  (xmlhttp.readyState == 4 && xmlhttp.status == 200)   ? func() : null ;  }xmlhttp.open("GET",source,true);xmlhttp.send();}

    wanna ask how to extend the scope of the variable sJSON to be global?

  11. When testing a webpage,I use IE9 Web Developer (press F12). Say,The first time I did something in Dreamweaver that uploaded the file later to the server. I pressed F12 andIE9 poped out and showed the result.I came back to DW and changed the codes a bit and came back to IE9.This time no matter how many times I had pressed F5 to refresh the webpage, it still stayed on the first result without changing. This does not happen ever in Firefox Console.Could there be a fix for this IE9 problem?

  12. To work with false
    var elem = "ThisElementID" function ajax()	{	xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;	xmlhttp.onreadystatechange = function()		{		if(this.readyState == 4 && this.status == 200)			{			jsonData = eval("("+xmlhttp.responseText+")");						}		}		xmlhttp.open("GET","json.json",false); // with false it pauses until value is returned before proceeding which may cause older browsers to lock.		xmlhttp.send(null);		return jsonData[1][2];		} window.onload= function()	{		document.getElementById(elem).innerHTML = ajax();	}

    another option

    var elem = "ThisElementID" function ajax()	{	xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;	xmlhttp.onreadystatechange = function()		{		if(this.readyState == 4 && this.status == 200)			{			jsonData = eval("("+xmlhttp.responseText+")");			document.getElementById(elem).innerHTML = jsonData[1][2];			}		}		xmlhttp.open("GET","json.json",true);		xmlhttp.send(null);				} window.onload= function()	{	 ajax()	}

    Yes, both work.
  13. Changin true to false (asyn) does not work. Yes, using a callback does the work. I didn't know how to use a callback, though.Just still don't understand why it works like return and why return does not work in this case.Just a typo (GetJsonData -> getJsonData)Thank you.

  14. The problem lies in, in my honest opinion, the way I return the xmlhttp.responseText.Just ask for help on how to return an ajax object.

    function ajax(){xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;xmlhttp.onreadystatechange = function(){   var jsonData = eval("("+xmlhttp.responseText+")");  (this.readyState == 4 && this.status == 200) ? alert(jsonData[0][0]) : null ;}xmlhttp.open("GET","json.json",true);xmlhttp.send(null);}

    This does the job, though.

  15. <script>function ajax(){xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;xmlhttp.onreadystatechange = function(){  var jsonData = eval("("+xmlhttp.responseText+")");  return (this.readyState == 4 && this.status == 200) ? jsonData[1][1] : null ;}xmlhttp.open("GET","json.json",true);xmlhttp.send(null);} window.onload function func(){document.getElementById(elem).innerHTML = ajax(); // return undefined}</script>

    Is there a syntax mistake that produces the undefined result?
×
×
  • Create New...