Jump to content


  • Content Count

  • Joined

  • Last visited

Community Reputation

0 Neutral

About Jay@TastefulTitles.com

  • Rank

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. I have several different pages that all need access to the same XML data file. I'm trying to use this fairly-standard function to load the file when the page loads. If I call getXMLFile() from within each pageLoadFunction, it works, but I'd like to use it to set up some other vars and avoid duplicating code. getXMLFile = function(callback) { var request = new XMLHttpRequest(); request.open("GET", "MyFileName.xml"); request.setRequestHeader("Content-Type", "text/xml"); request.onreadystatechange = function() { if (request.readyState == 4 && request.status == 200) { callback(request.responseXML); //Does code needed for all pages go here? Or where? } }; request.send(); } What I would like to do is put something like this in my HTML, using a callback to call the fill function after the file has loaded: <body onload="openXML(fillPageFunction)"> <!--There is a unique fill function for each page--> OpenXML() would call getXMLFile() to load the data, then make use of it. Procedurally, here is what I want to happen: Use getXMLFile() to load the data file Go ahead and render page HTML/CSS while file is loading When file is loaded, use it to fill some global vars needed on all pages Call a fill function to fill fields on the specific page I've spent hours studying manuals, tutorials, and examples on using callbacks, but my procedural brain still can't make them work. Can someone familiar with callbacks please show me how I could accomplish this? (Using named functions, rather than anonymous, would probably help to make it clearer to me.) To fill vars needed on all pages, would I put that code right after the line "callback(request.responseXML);"? Or where? To fill fields on specific page I would call a unique fillPage function for each page. Where would I do that? Can I make that function a param to openXML()?
  2. Solved! 😀 My brain had a sudden flash, so I decided to try something. Lo and behold, it worked! Here's what it took: $xml->getElementsByTagName("customers")->item(0)->replaceChild($node, $oldnode); I had not understood from the examples that I have to start with the parent node to replace a child node. Seems logical now that I've found it, but I sure didn't see it before. I doubt I could have got there if you hadn't found my var error, so many thanks for that.
  3. So I do. Thanks for catching that! I was so hopeful that correcting the var name would solve the problem, but there is no change. That line now looks like this: $node->replaceChild($node, $oldnode); //THIS LINE FAILS I think that may have been a relatively recent error, as I changed some var names to make it easier to compare with the example code, but its failure predates that. I hope you can take another look and find what else is screwed up. Logically, there must be some problem with the params I'm sending in this line, because the previous line executes without problem, and if I comment this line out, everything thereafter runs. My understanding of the DOM may still be a little fuzzy, so I can't see what isn't right. Using textContent on both vars shows exactly what I would expect. I even tried simply retyping the line, because I've seen instances where that made a difference, but not this time.
  4. At Ingolme's suggestion, I've switched from simpleXML to DOMXML for this because I need to be able to replace a node. I've done my best to follow PHP.com examples, and everything seems to work up until the point of the actual replacement. I hope you can spot what's keeping it from working. The vars found in the heredoc are assigned earlier from the customer input form. if $found is positive, it contains a cId (customer ID) that exists in the DB and should be updated with any new info (code shown). If negative, it's abs() is the cId for a new node to be added (code omitted here). In $xml, the <customer> node is child of <customers>, which is child of <root>. If I comment out the replaceChild line before the break, execution continues unhindered. Nodename reports "customer" and nodetype reports 'XML_ELEMENT_NODE' for both $node and $oldNode, so I don't know what is preventing the replacement. // SAVE CUSTOMER DATA // $found= $cId; $cId = abs($found); // Create DOM from data file $xml = new DOMDocument; $xml->formatOutput = true; $xml->load("Titles data.xml"); // Create node from customer input $str = <<<XX <customer> <cId>$cId</cId> <organization>$organization</organization> <website>$URL</website> <contact>$name</contact> <email>$email</email> <phone>$phone</phone> <addr>$address</addr> <city>$city</city> <state>$state</state> <zip>$zip</zip> <orders> <oId></oId> </orders> </customer> XX; $DOMChild = new DOMDocument; $DOMChild->loadXML($str); //turn $str into DOMDoc $node = $DOMChild->documentElement; //not sure if this is right, example suggests it is $node = $xml->importNode($node, true); //Is this necessary? examples suggest it is if ($found > 0) { //if positive, $found contains cId of desired customer $list = $xml->getElementsByTagName("cId"); //list of ID's to search for ($r = $list->length -1; $r > 0; $r--) { //find customer that fits cId if ($list->item($r)->nodeValue==$cId) { //when found, $r is index to customer node list $oldnode=$xml->getElementsByTagName("customer")->item($r);// get node to be replaced $msg=$r."—"; //$msg is included in email sent later; shows arrival here $node->replaceChild($node, $oldNode); //THIS LINE FAILS, HALTING EXECUTION break; } } }
  5. I like to use this hack when adding values in JS: sum = -(-x - y); It eliminates the ambiguous '+' operator altogether, along with any type confusion. It can also be put into a function: function add(a, b) {return -(-a - b)} sum = add(x, y); This may be slightly less efficient than declaring the var as numeric, but I think it makes for neater code, and there are times we want to make use of JS's loose typing, so the var can be used as text in one place and numeric in another.
  6. I will look at it. Do you have an example of using DOMDocument? That might work just as well for me. Thanks!
  7. Okay, I'll hunt for a DW replacement. It has to run on OS10.11.6. Any recommendations? Meanwhile, I can upload and run the code regardless of the error message, but it doesn't seem to work. I was wondering if you could see a reason why. Is this a reasonable way to approach my goal? Some of the discussions I've read seem to imply I have to insert each tag separately, but I can't tell for sure.
  8. "Syntax error on line 1" is apparently how DW CS5 reports a syntax error anywhere in a PHP doc. The only thing on line 1 is "<?php". The error could be in line 12,345, and fixing it will make the error report go away. In this case, if I comment out the heredoc (lines 138-155), it reports "No syntax errors."
  9. I'm a novice to PHP. What I want to do is add a new customer record to my XML database. It looks as if a simple way to do this would be to create a string using heredoc, which will insert the variable values, then turn it into a simpleXML object: $xml=simplexml_load_file("Test data.xml"); //load XML file // these $vars are all defined from form input in previous code $str= <<<EOS <!----> <customer> <cId>$Customer</cId> <organization>$organization</organization> <website>$URL</website> <contact>$name</contact> <email>$email</email> <phone>$phone</phone> <addr>$address</addr> <city>$city</city> <state>$state</state> <zip>$zip</zip> </customer> EOS; $new = new SimpleXMLElement($str); //turn $str into SimpleXML element $xml->addChild("customer",$new); //add it to DB My first question is whether DreamWeaver CS5 will allow me to do this. It gives me the "syntax error on line 1." Is it able to recognize heredoc? Second, am I approaching this the right way? I'm sure this is something that is done regularly for myriad websites, but I haven't found any clear direction on the best way to handle it. Finally, if this is the right approach, what am I missing to make it work?
  10. Most of my product pages include a link to a window showing the text of my translation, formatted with CSS to appear as a series of scrolling "slides." Each "slide" is a separate <div> containing 2, 1, or 0 <p>'s. The number of slides can range from ~25 to ~1,000. On some of the larger ones, I'm wondering whether there is an advantage to splitting the text HTML between two separate shell divs? If I put the first 50 or so slides in one <div>, will it load and display faster than if I put all 800 slides in the same <div>? I'll try to illustrate: <body> <div class="shell" onclick="window.close()"> <div><p>Title</p><p>by Composer></p></div> <!-- ~800 more divs here --> <div><p>Translation credit</p><p>Titles credit></p></div> </div> </body> OR... <body> <div class="shell" onclick="window.close()"> <div><p>Title</p><p>by Composer></p></div> <!-- ~50 divs here --> </div> <div class="shell" onclick="window.close()"> <!-- ~750 more divs here --> <div><p>Translation credit</p><p>Titles credit></p></div> </div> </body> The CSS makes the break between the shell divs invisible. The only difference would be whether one would display faster. Presumably if the first div were displayed before the second (currently not visible) were parsed, the second would be completed well before the user finished scrolling through the first, but I don't know whether it works that way or not. To see live examples, click on "View supertitles" at the bottom of these two windows. I have split the divs in the first (811 slides), not in the second (885 slides). https://tastefultitles.com/Catalog.html?num=4 https://tastefultitles.com/Catalog.html?num=25
  11. A button in my window opens a new, narrow window centered on the screen. window.open("/myWindow.html", "_blank", width="400", height=screen.height, top=0, left=(screen.width / 2 - 200)); My naive assumption was that, on a large screen (laptop or larger), this would create a window 400px wide. On my 13" macbook, according to the information in the Safari web inspector, it creates a window only 347.826px wide. The CSS of the new window sets a background, then creates a <div class="shell">, 300px wide. The setting of margin:0 auto is meant to center the "shell" in the new window. body { background-color:#C03; background-image:url(../images/Curtain%20narrow.jpg); overflow-x:hidden; } div.shell { background-color:transparent; color:white; font-family:Arial, Helvetica, sans-serif; font-size:16.5px; font-weight:600; height:auto; margin:0 auto; min-height:90vh; overflow:hidden; text-align:center; } What happens is that the 300px shell is drawn tight against the right edge of the narrower-than-specified window, and can be pulled back and forth. If I manually resize the window to 400px it looks and works as intended. Should I focus on getting the window to draw the right size? Attempt to resize it on open? Or is there some different setting that would give me what I want? On my iPhone 6s, The window fits the screen width and the shell div is centered. What am I missing to get the same results on the larger screen?
  12. Good grief! I can't believe I looked at that for so many hours without seeing it. It sure helps to have another set of eyes. Many thanks!
  13. I'm trying to fill a table on my HTML page with values from my xml data file. Var v is an HTMLCollection object containing these nodes extracted from the xml file: <pubisher>Peters</pubisher> <translator>Jay Reeve</translator> <slidecount>885</slidecount> <pagecount>189</pagecount> <other></other> I'm using this chunk of code. The vars are all local and declared earlier. for (r=0;r<v.length;r++) { str=v[r].innerHTML; //str gets the value "Peters" if (str>"") { fld=v[r].nodeName; //fld gets the value "publisher" x=document.getElementById(fld); //x is "undefined" x.style.display="block"; x.childNodes[1].innerText=str; } } On the first pass, x is supposed to reference the first row from this HTML table: <table id="details"> <tr id="publisher"> <td> Publisher:</td><td></td></tr> <tr id="translator"><td> Translator:</td><td></td></tr> <tr id="slidecount"><td>Slide count:</td><td></td></tr> <tr id="pagecount"> <td> Page count:</td><td></td></tr> <tr id="other"> <td> Other:</td><td></td></tr> </table> The debugger shows these values as they should be: fld: "publisher" str: "Peters" but x=document.getElementById(fld) is left undefined and throws an error. If I replace the variable fld with the string "publisher", it works fine. The table row becomes visible and the empty cell gets the value "Peters". So is there a restriction I don't know on using variables for Id's? Or Id's for table rows? I'm pretty sure I've done it before, but I can't see why this is failing. Is fld a restricted keyword? Could there be a type issue? I tried splicing quotes onto the fld string, but that didn't help. I don't think it can have anything to do with the fact that this is part of an anonymous callback function that runs after the file is loaded where v gets its value. Any insight would be most appreciated.
  14. It turns out I had merely neglected to include this line in my code: <meta name="viewport" content="width=device-width, initial-scale=1" /> Doing that took care of the problem. You'd think I would know better by now, but having never run into that before, it took me quite a while to find it.
  15. I thought I had fixed the missing px, but hadn't. Thanks for pointing it out. I have not heard of the security problem before, nor the simple fix. Thanks! Would you be interested in explaining how that helps? What could some miscreant do with my folders list, and how does the empty index.html prevent that?
  • Create New...