unplugged_web Posted March 26, 2013 Share Posted March 26, 2013 I've got a function called currentValues() that gives the information I need in the following format [Name, Initial Value, Total Amount]. An example of what I get is: var currents="[Jo,0,1250.21][ian,10,2555.11][Fred,2,186.36][sue,0.30,1157.56]"; function currentValues() { $("#currentinfo").html(current s); } But I don't know how to get it so that I can display just the Total Amount of say Jo. I don't even know where to start so would be grateful for any pointers. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 26, 2013 Share Posted March 26, 2013 Is that actually a string like you showed or does it return an array? Link to comment Share on other sites More sharing options...
unplugged_web Posted March 27, 2013 Author Share Posted March 27, 2013 Is that actually a string like you showed or does it return an array?When I go to the url that returns that, that's exactly what I get. I can't give the url though as it's password protected Link to comment Share on other sites More sharing options...
thescientist Posted March 27, 2013 Share Posted March 27, 2013 you didn't answer the question. is it returning a string, or an array? log it in the console in Chrome or something similar, and if it's an array, it should allow you to expand it. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 27, 2013 Share Posted March 27, 2013 If it's a JSON string then you can convert it to an array and use it that way. Maybe paste exactly what the page returns. Link to comment Share on other sites More sharing options...
unplugged_web Posted March 28, 2013 Author Share Posted March 28, 2013 (edited) Sorry I wasn't very clear, it's a string. When I open the console there's nothing to expand. This is exactly what I get: var currents="[Jo,0,1250.21][ian,0.10,2578.30][Fred,2.00,249.25][sue,0.30,1157.56][Chris,0.60,0.15][John,3.00,4742.60][Carl,0.50,0.00][Amanda,0.50,1356.89][Zoe,0.50,549.28][Jo,0.50,0.00][Jen,0.50,0.00][Jo,1.00,288.27][Tracey,1.00,1499.20][Colin,1.00,0.00][simon,1.00,0.00][William,1.00,0.00][Ty,5.00,4872.86][Red,2.00,249.27][ian,2.00,1023.41][Jeff,2.00,0.00][Kevin,2.00,0.00][Carol,2.00,0.00]"; function currentValues() { $("#currentinfo").html(currents); } They're sorted by Name, Initial Value, Total Amount. I need to get the Total Amount depending on what the Name are Initial Values are. So for example I want to Total Amount of Jo when the Initial Value is 0.50, but not when it's 0 Edited March 28, 2013 by thehappyappy Link to comment Share on other sites More sharing options...
justsomeguy Posted March 28, 2013 Share Posted March 28, 2013 Can you change how it prints that? That is not the easiest string to parse. It would be a lot easier to put that in a format to start with that Javascript can use immediately. Link to comment Share on other sites More sharing options...
davej Posted March 30, 2013 Share Posted March 30, 2013 <!DOCTYPE html><html lang="en"><head><title>split string</title><style>.hdr {font-weight: bold;color: #0f0;}</style><script type="text/javascript" src="jonerror.js"></script><script>window.onload = function() {document.getElementById('out').innerHTML = '';process();}var currents="[Jo,0,1250.21][Ian,10,2555.11][Fred,2,186.36][Sue,0.30,1157.56]";function process() {var clean = currents.replace(/\[/g,'');var arr = clean.split(']');for (var i=0 ; i<arr.length-1 ; i++) {var temp = arr[i].split(',');document.getElementById('out').innerHTML += '<span class="hdr">Name:</span> '+ temp[0];document.getElementById('out').innerHTML += ' <span class="hdr">Initial Value:</span> '+ temp[1];document.getElementById('out').innerHTML += ' <span class="hdr">Total Value:</span> '+ temp[2] +'<br/>';}}</script></head><body><h2>Customer Values</h2><div id="out"></div></body></html> Link to comment Share on other sites More sharing options...
unplugged_web Posted April 2, 2013 Author Share Posted April 2, 2013 Can you change how it prints that? That is not the easiest string to parse. It would be a lot easier to put that in a format to start with that Javascript can use immediately. Unfortunately I have no control over how the script is printed at all. I just have a url that I need to get the information from. Link to comment Share on other sites More sharing options...
unplugged_web Posted April 2, 2013 Author Share Posted April 2, 2013 <!DOCTYPE html><html lang="en"><head><title>split string</title><style>.hdr {font-weight: bold;color: #0f0;}</style><script type="text/javascript" src="jonerror.js"></script><script>window.onload = function() {document.getElementById('out').innerHTML = '';process();}var currents="[Jo,0,1250.21][Ian,10,2555.11][Fred,2,186.36][Sue,0.30,1157.56]";function process() {var clean = currents.replace(/\[/g,'');var arr = clean.split(']');for (var i=0 ; i<arr.length-1 ; i++) {var temp = arr[i].split(',');document.getElementById('out').innerHTML += '<span class="hdr">Name:</span> '+ temp[0];document.getElementById('out').innerHTML += ' <span class="hdr">Initial Value:</span> '+ temp[1];document.getElementById('out').innerHTML += ' <span class="hdr">Total Value:</span> '+ temp[2] +'<br/>';}}</script></head><body><h2>Customer Values</h2><div id="out"></div></body></html> Thank you that's great, but can I ask how I get the customer value for just one person depending on what their initial value is? Also I can't change how the script is printed at all. Link to comment Share on other sites More sharing options...
scott100 Posted April 2, 2013 Share Posted April 2, 2013 (edited) Hi, if you add a couple of paramaters to the process function - name and initialValue - you can then call it like so: process("Jo","0.50");orprocess("Jo","0"); The if statement can check if the paramters are found and the initial value not eqaual to 0 if ((temp[0] == name) && (initialValue != "0")) { alert("Found " + name + " and initial value is not 0 - total Amount is " + temp[2] ); } <!DOCTYPE html><html lang="en"><head><title>split string</title><style>.hdr {font-weight: bold;color: #0f0;}</style><script type="text/javascript" src="jonerror.js"></script><script>window.onload = function() {document.getElementById('out').innerHTML = ''; process("Jo","0.50"); // This will fireprocess("Jo","0"); // This wont fire }var currents="[Jo,0,1250.21][Ian,10,2555.11][Fred,2,186.36][Sue,0.30,1157.56]"; function process(name, initialValue) { var clean = currents.replace(/\[/g,''); var arr = clean.split(']'); for (var i=0 ; i<arr.length-1 ; i++) { var temp = arr[i].split(','); if ((temp[0] == name) && (initialValue != "0")) { alert("Found " + name + " and initial value is more than 0 - total Amount is " + temp[2] ); } //document.getElementById('out').innerHTML += '<span class="hdr">Name:</span> '+ temp[0]; //document.getElementById('out').innerHTML += ' <span class="hdr">Initial Value:</span> '+ temp[1]; //document.getElementById('out').innerHTML += ' <span class="hdr">Total Value:</span> '+ temp[2] +'<br/>'; }}</script></head><body><h2>Customer Values</h2><div id="out"></div></body></html> Edited April 2, 2013 by scott100 Link to comment Share on other sites More sharing options...
unplugged_web Posted April 2, 2013 Author Share Posted April 2, 2013 Thanks, that's exactly what I need, but can I do that when I don't have any control over what's printed at all? All I have is a url that gives me the values? Link to comment Share on other sites More sharing options...
scott100 Posted April 2, 2013 Share Posted April 2, 2013 (edited) What does the url look like? you could try var currents=window.location.href.toString() instead of this var currents="[Jo,0,1250.21][ian,10,2555.11][Fred,2,186.36][sue,0.30,1157.56]"; Edited April 2, 2013 by scott100 Link to comment Share on other sites More sharing options...
unplugged_web Posted April 2, 2013 Author Share Posted April 2, 2013 (edited) Thanks I tried that, but it didn't work. I've now got this: <script>window.onload = function() {document.getElementById('out').innerHTML = '';process();}var currents=window.location.href.toString('https://www.domain.com/password/users.aspx')function process() {var clean = currents.replace(/\[/g,'');var arr = clean.split(']');for (var i=0 ; i<arr.length-1 ; i++) {var temp = arr[i].split(',');document.getElementById('out').innerHTML += '<span class="hdr">Name:</span> '+ temp[0];document.getElementById('out').innerHTML += ' <span class="hdr">Initial Value:</span> '+ temp[1];document.getElementById('out').innerHTML += ' <span class="hdr">Total Value:</span> '+ temp[2] +'<br/>';}}</script> Then within the actual page I've got: <div id="out"></div> but it doesn't show anything at all. I tried putting some random text in the 'out' div to make sure it was showing and it was but it's not showing the values. Edited April 2, 2013 by thehappyappy Link to comment Share on other sites More sharing options...
scott100 Posted April 2, 2013 Share Posted April 2, 2013 its quite literally trying to split the url:https://www.domain.com/password/users.aspxnot what is returned from the page users.aspx Link to comment Share on other sites More sharing options...
unplugged_web Posted April 2, 2013 Author Share Posted April 2, 2013 its quite literally trying to split the url:https://www.domain.c...word/users.aspx not what is returned from the page users.aspxSorry I don't understand? I couldn't post the real url because it contains the password to get the details, but when I got to the url it prints this:var currents="[Jo,0,1250.21][ian,0.10,2578.30][Fred,2.00,249.25][sue,0.30,1157.56][Chris,0.60,0.15][John,3.00,4742.60][Carl,0.50,0.00][Amanda,0.50,1356.89][Zoe,0.50,549.28][Jo,0.50,0.00][Jen,0.50,0.00][Jo,1.00,288.27][Tracey,1.00,1499.20][Colin,1.00,0.00][simon,1.00,0.00][William,1.00,0.00][Ty,5.00,4872.86][Red,2.00,249.27][ian,2.00,1023.41][Jeff,2.00,0.00][Kevin,2.00,0.00][Carol,2.00,0.00]";function currentValues() {$("#currentinfo").html(currents);} Link to comment Share on other sites More sharing options...
scott100 Posted April 2, 2013 Share Posted April 2, 2013 (edited) ok, so you go to this pagewww.domain.com/password/users.aspx you view source and you see var currents="[Jo,0,1250.21][ian,0.10,2578.30][Fred,2.00,249.25][sue,0.30,1157.56][Chris,0.60,0.15][John,3.00,4742.60][Carl,0.50,0.00][Amanda,0.50,1356.89][Zoe,0.50,549.28][Jo,0.50,0.00][Jen,0.50,0.00][Jo,1.00,288.27][Tracey,1.00,1499.20][Colin,1.00,0.00][simon,1.00,0.00][William,1.00,0.00][Ty,5.00,4872.86][Red,2.00,249.27][ian,2.00,1023.41][Jeff,2.00,0.00][Kevin,2.00,0.00][Carol,2.00,0.00]";? Do you have any control over this page (users.aspx)? I notice you said earlier Thanks, that's exactly what I need, but can I do that when I don't have any control over what's printed at all? All I have is a url that gives me the values?Do you only have read access to this page then? You can't actually edit the files on the server? Edited April 2, 2013 by scott100 Link to comment Share on other sites More sharing options...
unplugged_web Posted April 2, 2013 Author Share Posted April 2, 2013 No, unfortunately I have no control over that page at all. Link to comment Share on other sites More sharing options...
scott100 Posted April 2, 2013 Share Posted April 2, 2013 (edited) If you have no control over the page then you can't edit it. You could try injecting in a little piece of javascript but the change wont be permanent and only you would see ithttp://www.wikihow.c...ript-Injections if you browsed to the page, in IE/Chrome, type this into the address bar and hit return (don't copy and paste as the browser removes the javascript:word) javascript:;alert(currents); this may or may not be of any use. Edited April 2, 2013 by scott100 Link to comment Share on other sites More sharing options...
justsomeguy Posted April 2, 2013 Share Posted April 2, 2013 If that page is on a different domain then you can't use Javascript to get it at all, ajax does not work to get content on a different domain. Maybe you could create an invisible iframe and load the document into that iframe, then get the contents of it with Javascript, but that might also be restricted if it's on a different domain. Link to comment Share on other sites More sharing options...
unplugged_web Posted April 3, 2013 Author Share Posted April 3, 2013 If that page is on a different domain then you can't use Javascript to get it at all, ajax does not work to get content on a different domain. Maybe you could create an invisible iframe and load the document into that iframe, then get the contents of it with Javascript, but that might also be restricted if it's on a different domain.Is it possible then to get the results into a database? I can then get just the results I need if everything is stored in it's own field? Link to comment Share on other sites More sharing options...
thescientist Posted April 3, 2013 Share Posted April 3, 2013 You might have to looking into how to make this reauest on the server side using cURL, passing your username and password. Then you could make an AJAX request to a PHP script that makes the cURL request, handles parsing the data into something more JS friendly (like an array or object) and returns that. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 8, 2013 Share Posted April 8, 2013 It may also be possible to load that using a script tag, e.g.: <script type="text/javascript" src="http://path/to/output"></script> Link to comment Share on other sites More sharing options...
unplugged_web Posted April 10, 2013 Author Share Posted April 10, 2013 (edited) I've managed to get the results, but for some reason it doesn't show any results at all in IE and I'm not sure why. It works in every other browser, but just not IE. This is what I'm using to get the results. The <head> section had this: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> </script><script src="js/users.js" type="text/javascript"></script> <script type="text/javascript"> var imagesURL = localSt.iUrl(); function call(url, parameters, callback) { var localTimeout = 10000; $("#loader").show(); $.ajax({ type: 'POST', url: url, data: parameters, timeout: localTimeout, success: function(data) { $("#loader").hide(); var obj = eval('(' + data + ')'); if (obj.ER) { var header = ""; var msg = "Sorry there seems to be a communication error (" + obj.ER + ").<br/>Please log in again."; //Error detail may be in PU if (obj.ER == "103") { header = obj.PU.HD; msg = obj.PU.BD; } else if (obj.ER == "1007") { //authentcation failed header = obj.PU.HD; msg = obj.PU.BD; } $(this).attr("disabled", false); showMessage(header, msg); } else { if (obj.PU) { var msg = ""; var header = "" if (obj.PU.HD) { header = obj.PU.HD; } showMessage(header, obj.PU.BD, function() { callback(data); }); } else { callback(data); } } } }); } function loadtotals() { call("https://my.domain.com/server/passkey.ashx", { JL: 0 }, function(data) { var divIdentifier; var obj = eval('(' + data + ')'); $.each(obj.JL, function() { divIdentifier = ""; //keys: userID,currentID,lifetimeID,total switch (this.userID) { case 2: divIdentifier = "#Jo"; break; case 5: divIdentifier = "#Fred"; break; case 1000: divIdentifier = "#Sue"; break; case 1001: divIdentifier = "#Chris"; break; case 1002: divIdentifier = "#John"; break; } if (this.userID >= 1000) { switch (this.lifetimeID) { case 4: divIdentifier += "_0"; break; case 5: divIdentifier += "_2"; break; case 7: case 9: divIdentifier += "_3"; break; } } if (this.userID == 1000) { switch (this.currentID) { case 0: divIdentifier += "_1h"; break; case 1: divIdentifier += "_2h"; break; case 2: divIdentifier += "_3h"; break; } } $(divIdentifier).html("£" + this.total); }); } ); } function showApp() { $("#loader").hide(); } $(document).ready(function() { if (navigator.userAgent.match(/Android/i)) { $("body").addClass("android"); } if (navigator.userAgent.indexOf("iPhone OS") > -1) { if (navigator.userAgent.indexOf("Version/5.1") > -1) { $("body").addClass("iphone4"); } $("body").addClass("iphone"); } $("a").click(function(event) { event.preventDefault(); parent.$("#newuserID").val(0); window.location = $(this).attr("href"); }); loadtotals(); showApp(); }); </script> And within the <body> section I'm displaying the results by using this: <div style="float: right; margin: 2px 149px 0 0;"><script type="text/javascript">document.write('<div id="Jo">');</script><div id="Jo_0"></div><div id="Jo_2"></div><div id="Jo_3"></div></div> <div class="clear"></div> <script type="text/javascript">document.write('<div id="Fred">');</script><div id="Fred_0"></div><div id="Fred_1"></div><div id="Fred_2"></div></div> <div class="clear"></div> </div><div style="float: left; margin: 194px 0 0 185px;"><script type="text/javascript">document.write('<div id="Sue">');</script><div id="Sue_0_1h"></div><div id="Sue_0_2h"></div><div id="Sue_0_3h"></div></div><script type="text/javascript">document.write('<div id="Sue">');</script><div id="Sue_1_1h"></div><div id="Sue_1_2h"></div><div id="Sue_1_3h"></div></div><script type="text/javascript">document.write('<div id="Sue">');</script><div id="Sue_3_1h"></div><div id="Sue_3_2h"></div><div id="Sue_3_3h"></div></div> <div class="clear"></div> </div></div><div style="height: 80px;"><script type="text/javascript">document.write('<div id="Chris_0">');</script><div id="Chris"></div></div> <div class="clear"></div> <script type="text/javascript">document.write('<div id="John_0">');</script><div id="John"></div></div> <div class="clear"></div></div> Edited April 10, 2013 by thehappyappy Link to comment Share on other sites More sharing options...
thescientist Posted April 10, 2013 Share Posted April 10, 2013 (edited) have you checked for errors in the console? note: the use of document.write is highly discouraged. Using DOM methods that reference an element and inject text like with innerHTML or using create/append element are considered best practice. Edited April 10, 2013 by thescientist Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now