SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Print an Object

  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Print and Retrieve Javascript Object

    Hello

    let say with a javascript Object called linktarget it is made of a source-page with 4 to 5 links to targeted-pages that talk about the same topic than the source-page (you might have seen that already ).

    i have an admin interface of let say some sort of a cms that let me play with every linktarget i.e. source-page and its targeted pages (so i can add or remove a target from a certain linktarget object) but the first thing i want to do is to display every LinkTarget object in the system (an array of targetlink object) so on my administration homepage i would have in a 2 columns table where every linktaget object occupies a single row where one cell contains info about the source-page and a second cell contains all the info about the targeted-pages linked to that source.

    I am looking for help to write the function that access the object, retrieve the properties (source_url, source_title, source_date, source_category, target_title, target_date) and display them in the 2 columns html table i described described earlier...

    Thanks for the help!

    here is what i started:
    Code JavaScript:
    document.getElementById('SmartLinks');
    document.write('SmartLinks.Document_Source.URL');
    document.write('SmartLinks.Document_Source.Titre');
    document.write('SmartLinks.Document_Source.Date');
    document.write('SmartLinks.Zone_Cible[]');
    document.write('SmartLinks.Table_Cible.Document_Cible.Titre');
    document.write('SmartLinks.Table_Cible.Document_Cible.Date');
    Last edited by motion-ex; Jul 16, 2007 at 07:37.
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  2. #2
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in other words let say you have an object named person with properties: age, sex, location, job, phone...

    how do you get the properties of the object and then display them in an html table for example?
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by motion-ex View Post
    in other words let say you have an object named person with properties: age, sex, location, job, phone...

    how do you get the properties of the object and then display them in an html table for example?
    You don't use document.write. You'll have to use either the DOM API, or inject HTML with the innerHTML property of some element. For example, using the DOM API:
    HTML Code:
    <html>
    <head>
    <script>
    var person = {age: 30, sex: "male", location: "Wherever I please"};
    window.onload = function() {
      var output = document.getElementById("output");
      var table = output.appendChild(
        document.createElement("table"));
      var tbody = table.appendChild(
        document.createElement("tbody"));
      var tr = tbody.appendChild(
        document.createElement("tr"));
    
      for (var key in person) {
        var td = tr.appendChild(
          document.createElement("td"));
        td.appendChild(document.createTextNode(person[key]));
      }
    }
    </script>
    </head>
    <body>
    <div id="output"></div>
    </body>
    </html>
    There are much more elegant ways of doing the same thing, but this is the dogmatic approach.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much indeed i realised my mistake earlier when going through different tutorials.... when is it ok to use document.write though?
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by motion-ex View Post
    when is it ok to use document.write though?
    Never. It works in a completely different way than all other javascript functions, and you always have alternatives. In ye old days it had greater support by browsers, so you could use it for making code more portable. This isn't an issue today though, so just stear clear of it.

  6. #6
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you for this advice i will indeed stay away from document.write... it's just that in the project i do for my internship i actually have to take some of the code that was writen by the previous developper and it has a lot of document.write... well anyway i see that javascript is not a little toy for web developper it's actually a real language with its own complexities!
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  7. #7
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One thing also... i would like to know if JSON is to be used onlyas a replacement to XML i.e. in other circountances i can not use JSOn to represent a javascript objezct for example?
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  8. #8
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JSON is not only used as a replacement to XML. JSON is basically a JavaScript object defined with a certain syntax.

  9. #9
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you indeed i wanted to use that syntax to define objects in my scripts... not sure of its usefulness hough but since json will be used as the data exchange formats in the app. we're building i thought it will be better to go on writing object in that format in the entire script
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  10. #10
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what's the best way to generate an object from info retrieved in a database?
    the var theobject = new object way or the var theobject = function(theobject)
    ?
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  11. #11
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by motion-ex View Post
    what's the best way to generate an object from info retrieved in a database?
    Assuming you're using PHP, load the data into an associative array, and use the json-extension to transform it into JSON. It's almost a one-liner, once you have it figured out.

  12. #12
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    indeed! thank u
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  13. #13
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    function TagLink(url,title,date,zone,tableCible){
    	this.url = url;
    	this.title = title;
    	this.date = date;
    	this.zone = zone;
    	this.tableCible= tableCibe; //where tablecible is a 2 dimension array
    }
    is it ok to write this?
    On a PHP/Java/XML/Javascript/MySQL internship right now!


Bookmarks

Posting Permissions

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