Results 1 to 12 of 12

Thread: I need someone who knows something about Javascript

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member beelzebub's Avatar
    Join Date
    Aug 2004
    Location
    California
    Posts
    223

    I need someone who knows something about Javascript

    OK so I wrote a script that traverses an xml document. Click here to download the script. This script works fine when a <URL> element is in the <STORY> element, but when a <URL> does not exist the script stops writing everything after that point in the XML. What I was trying to do was add a simple if routine to the script to check for a <URL> and if not present skip it and go on. Unfortunelty I dont know very much javascript (which is evident by my use of var b = tree.childNodes[i].childNodes[2]) so I failed. Can anyone help me write the routine to check for the <URL>.

    the if statement is highlighted in red here.

    function traverse(tree) {
    for(var i=0; i<tree.childNodes.length;i++){
    document.write('<TR><TD>')
    document.write('<DIV CLASS = "header">')
    document.write(tree.childNodes[i].childNodes[0].text)
    document.write('</DIV>')
    document.write('<DIV CLASS = "general">')
    document.write(tree.childNodes[i].childNodes[1].text)
    document.write('</DIV>')
    var b = tree.childNodes[i].childNodes[2]
    if(b > 1){
    document.write('<DIV>')
    document.write('<A HREF = "'+tree.childNodes[i].childNodes[2].text+'" TARGET = "_blank" CLASS = "url">'+tree.childNodes[i].childNodes[2].text+ '</A>')
    document.write('</DIV>')}
    document.write('</TR></TD>')

    }

    }


    Thank you in advance.

  2. #2
    The Beast Master TZ Veteran PIPER's Avatar
    Join Date
    May 2002
    Location
    Florida
    Posts
    1,055
    Not into java....unless it is in a cup....lol

    this might help you a bit.

    http://www.webteacher.com/javascript/

  3. #3
    Senior Member beelzebub's Avatar
    Join Date
    Aug 2004
    Location
    California
    Posts
    223
    no it doesnt, thank you anyway

  4. #4
    Senior Member beelzebub's Avatar
    Join Date
    Aug 2004
    Location
    California
    Posts
    223
    OK this is a bump that actually has a purpose other than getting people to notice. I have modified the script and the xml file. The page itself now looks like this with the modified code.

    The question itself remains the same.
    Can anyone help me write the routine to check for the <URL>?

  5. #5
    Senior Member beelzebub's Avatar
    Join Date
    Aug 2004
    Location
    California
    Posts
    223
    Ok so i solved the problem, I will show the solution as a reference for anyone else who has this problem.

    So my solution is not an all around solution. I cant get the getElementsByTagName('tag') function to work so I had to refer to the URL tag by postion not name. This will all make sense in a minute.

    So here is the new code:

    <script language="JavaScript">
    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    function loadXML(xmlFile) {
    xmlDoc.async="false";
    xmlDoc.onreadystatechange=verify;
    xmlDoc.load(xmlFile);
    }

    function verify() {
    if(xmlDoc.readyState!=4)
    return false;
    }

    function traverse(tree) {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate();
    for(var i=0; i<tree.childNodes.length;i++){
    document.write('<TR><TD>');
    var date = tree.childNodes[i].getAttribute("EXPIRES").split("/");
    var color,text;
    if(date[2] < year){
    if(date[0] < month){
    if(date[1] < day){color = "#E03127"; text = "white";}}}
    else{color = "silver"; text = "#000000";}
    document.write('<DIV CLASS = "header" STYLE = "background-color:'+color+'; color:'+text+'">'+tree.childNodes[i].childNodes[0].text+'</DIV>');
    document.write('<DIV CLASS = "general">'+tree.childNodes[i].childNodes[1].text+'</DIV>');
    for(var n=0; n<tree.childNodes[i].childNodes.length;n++){
    if(tree.childNodes[i].childNodes[n].tagName == 'URL'){
    document.write('<DIV><A HREF = "'+tree.childNodes[i].childNodes[2].text+'" TARGET = "_blank">'+tree.childNodes[i].childNodes[3].text+'</A></DIV>');

    }
    }
    document.write('<HR></TD></TR>');
    }


    }

    function initTraverse(file) {
    loadXML(file);
    var doc=xmlDoc.documentElement;
    traverse(doc);
    }

    initTraverse("events.xml");
    </script>

    I added a for loop (identified in red) that will cycle through all of the child nodes in the <EVENT> tag (unfortunetly this causes the script to be slower). In that for loop I added an if statement that tells the browser to write an <A HREF> if the child node it is currently cycling through titled URL. Otherwise it does nothing. This has two benefits. One I dont need to specify another if statement that says dont write anything when it encounters the URL_TITLE tag, and it does nothing if there is no URL tag. allowing me to omit it when ever I please. So the reason why it is not all around is because instead of the script saying <A HREF = "'...getElementsByTagName('URL')'"> (insterting the text of the URL tag based on tag name) it insterts the text of the URL based on the URLs position in the XML document. So, I am unable to move the URL tag (though i can omit it) or insert any new tags above it. Feel free to tell me how to change that, by the way?

    Oh ya if you were wondering the date stuff at the top of the original for loop checks to see if the event is old or upcoming. I added an attribute to <EVENT> called EXPIRES which stores a date (mm/dd/yyyy) this date is split at each /. The if statements compair mm to the current month and so forth. If the date is still younger than the expiration date it gives the <div> for the events title a silver background otherwise it give it a redish background with white text.

    Click here to see the end result of my labors.
    Last edited by beelzebub; July 24th, 2006 at 22:19 PM.

  6. #6
    Security Intelligence TZ Veteran cash_site's Avatar
    Join Date
    Jul 2002
    Location
    Software Paradise
    Posts
    3,385
    Well good to hear that you got some solution working. I just checked that final link, 8pm PCT, there were a few bugs with the menu on the left, roll-over color remains, and also, there is no body (ie where is that tree control?)... discard if you were working on the page at that time...LOL.


    Edit: I prolly dont understand what u were trying to code up in the first place, is the code above just for the Menu system?
    Last edited by cash_site; July 25th, 2006 at 02:18 AM. Reason: FYI

    --- 0wN3D by 3gG ---

  7. #7
    Senior Member beelzebub's Avatar
    Join Date
    Aug 2004
    Location
    California
    Posts
    223
    no the code is to traverse and display an xml document in HTML. The menu you speak of is in the HTML outlined by the comment <!--Navigation Menu Begins Here. --> What browser are you using? Firefox i presume.
    Last edited by beelzebub; July 25th, 2006 at 07:52 AM.

  8. #8
    Senior Member beelzebub's Avatar
    Join Date
    Aug 2004
    Location
    California
    Posts
    223
    I think the reason why your browser is not displaying it is because of this var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    function loadXML(xmlFile) {
    I think it only works for ie, was going to make it cross platform later.

    EDIT: I just checked it with firefox, that menu error is wierd, anyone out there know how to fix that?

    I dont understand why it wouldnt load with the background color. Does the BGCOLOR = "######" attribute not work in tables in firefox?

    Wow I just went through the page in firefox and most of my formating is messed up. But it is like CSS formatting. Dang, I wonder if I had programed this and checked it using firefox, would it also have worked in IE?
    Last edited by beelzebub; July 25th, 2006 at 07:46 AM.

  9. #9
    Senior Member beelzebub's Avatar
    Join Date
    Aug 2004
    Location
    California
    Posts
    223
    ok new problem I was trying to make this script work for Firefox but the xml load routing doesnt seem to agree with it. Here is the routine, everything in red is what I changed (important note: this script still works in IE):


    if(window.ActiveXObject){
    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");}
    else{ if (document.implementation&&document.implementation.createDocument){
    var xmlDoc=document.implementation.createDocument("","doc",null);}}


    function loadXML(xmlFile) {
    xmlDoc.async="false";
    xmlDoc.onreadystatechange=verify;
    xmlDoc.load(xmlFile);
    }

    function verify() {
    if(xmlDoc.readyState!=4)
    return false;
    }
    function traverse(tree) {}
    function initTraverse(file) {
    loadXML(file);
    var xmlNew=xmlDoc.documentElement;
    traverse(xmlNew);
    }

    initTraverse("events.xml");

    anyone have any suggestions?
    Last edited by beelzebub; July 25th, 2006 at 19:32 PM.

  10. #10
    Security Intelligence TZ Veteran cash_site's Avatar
    Join Date
    Jul 2002
    Location
    Software Paradise
    Posts
    3,385
    yeah, the screen is totaly different in IE compared to FFox... i can see the body now in IE... so, you want the Javascript to make <a tags> inside the body? Can u post a snipet of the XML file?

    Im pretty sure there are built-in FFox or IE code api's etc for XML traversing etc... I think FFox uses the DomDoc ...

    Did u want to hand code the page, surely Dreamweaaver or FrontPage (since ur using IE anyway), could easily interface with XML files...

    --- 0wN3D by 3gG ---

  11. #11
    Senior Member beelzebub's Avatar
    Join Date
    Aug 2004
    Location
    California
    Posts
    223
    Thanks for paying attention cash site, i almost feel like i am talking to myself.

    So I guess I should explain everything, I have been using one script to represent problems shared by 3 scripts on 3 different pages. I was doing this because the scripts all have the same function, to traverse an XML doc and write it to the screen when a their respective HTML page is opened (though each script is different because each xml doc has a different structure).

    Here are the links to the pages that have the different scripts, side by side with the xml doc they display and the script in question used on each page (note: the scripts themselves are inline not accessed from seperate .js files so they can also be found in the HTML/I have privded them in .js files to allow you to view them seperate from the HTML):

    page 1 | xml doc | script
    page 2 | xml doc | script
    page 3 | xml doc | script

    Each of these pages works perfectly in IE and the problems discribed earlier in the thread are now resolved. All of the the problems described ealier were shared with the xml traversal script for each page.

    The current problem is the Mozilla incompatability of this page.

    (Chapter 1: HTML compatibility)
    I have realized that Mozilla is very strict about formatting many elements with one tag. Which is the problem with the navigation menu. In the <TABLE> tag for that menu there resides a BGCOLOR = "######" attribute (it looks like this <TABLE BGCOLOR = "######">......</TABLE>) . I did this to give the entire table including the cells a background. Unfortunetly Mozilla wants each cell to have the BGCOLOR attribute (looks like this <TD BGCOLOR = "######">....</TD>) instead, to say this another way Mozilla wont allow the BGCOLOR of the cells to be set in the <TABLE> tag only in the <TD> tags.

    So I now know how to solve my HTML compatibility issues. They are no longer a problem. The issue I dont know how to solve though is one with javascript .

    (Chapter 2: Javascript/XML compatibility)
    Here is the javascript problem. The scripts would work fine in Mozilla, except that the routine to load an XML file in Mozilla is different from the one in IE. I programed all of the scripts for IE.

    Oh ya here is the routine (it is common to every script, I am omitting the traverse function because it is different for each script):

    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    function loadXML(xmlFile) {

    xmlDoc.async="false";
    xmlDoc.onreadystatechange=verify;
    xmlDoc.load(xmlFile);
    }

    function verify() {
    if(xmlDoc.readyState!=4)
    return false;
    }

    function traverse(tree) {...}

    function initTraverse(file) {
    loadXML(file);
    var doc=xmlDoc.documentElement;
    traverse(doc);
    }

    initTraverse("events.xml");

    The part I have highlighted in red is the load command that makes these scripts only work for IE. Now, I have read in several place that all I need to do is modify

    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    function loadXML(xmlFile)

    to say

    var xmlDoc;
    if(window.ActiveXObject){
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");}
    else{ if (document.implementation && document.implementation.createDocument){
    xmlDoc=document.implementation.createDocument("","doc",null);}
    }

    The part I have highlighted in red is the routine that should make the scripts work in Mozilla. Unfortunetly this is not working, though the pages still load in IE. This is my current problem. All I need to know is what to write in order to make this work in both types of browser. I am willing to completely trash everything but the traverse function, if that is what it takes.

    Note: Each page linked to at the top has the updated script, with the Mozilla compatibility routine. And the XML files are the most recent versions.

    Did u want to hand code the page, surely Dreamweaaver or FrontPage (since ur using IE anyway), could easily interface with XML files...
    I pride myself on hand coding everything, I will never use a wysiwyg to make webpages, especially Dreamweaver. I have had so many problems with that piece of trash program in the past that I recommened people throw it out and buy an HTML coding book.

  12. #12
    Senior Member beelzebub's Avatar
    Join Date
    Aug 2004
    Location
    California
    Posts
    223
    OK the solutions to all the problems I had involving Firefox are here:
    http://forums.mozillazine.org/viewto...395758#2395758

    this is the completed site sans flash animations
    http://www.999webmasters.com/users/cferri39//index.html

Posting Permissions

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