how to retrieve the data in array with html element access?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harry

    how to retrieve the data in array with html element access?

    Hi Guys
    I dont really know how to do this: if there are a page of others,
    some data are embedded inside the data array like this:

    <script language="JavaS cript" type="text/javascript">
    <!--
    var jsData = new Array();
    jsData[0] = {bowl:"I", year:1967, winner:"Packers ", winScore:35,
    loser:"Chiefs", losScore:10};
    jsData[1] = {bowl:"II", year:1968, winner:"Packers ", winScore:33,
    loser:"Raiders (Oakland)", losScore:14};
    jsData[2] = {bowl:"III", year:1969, winner:"Jets", winScore:16,
    loser:"Colts (Balto)", losScore:7};
    jsData[3] = {bowl:"IV", year:1970, winner:"Chiefs" , winScore:23,
    loser:"Vikings" , losScore:7};
    jsData[4] = {bowl:"V", year:1971, winner:"Colts (Balto)", winScore:16,
    loser:"Cowboys" , losScore:13};
    --!>

    Now only interested in these data,I want to export it to some format
    that i like ,store somewhere else, How can I do this?

    I know inside javascript ,you just use it's variable name to access
    it ,but how about out side? just like we access html elements by it's
    tag name, can we just use some" xpath or DOM way" to access this
    array?

    especially I'm using ruby(and Hpricot) to process some page content,
    now the choice for me is just use some regular expression to deal it
    like a dead string, not an array.....reall y appriciate if someone give
    me a hint

    thanks !
  • SAM

    #2
    Re: how to retrieve the data in array with html element access?

    Harry a écrit :
    Hi Guys
    I dont really know how to do this: if there are a page of others,
    some data are embedded inside the data array like this:
    >
    <script language="JavaS cript" type="text/javascript">
    <!--
    var jsData = new Array();
    jsData[0] = {bowl:"I", year:1967, winner:"Packers ", winScore:35,
    loser:"Chiefs", losScore:10};
    jsData[1] = {bowl:"II", year:1968, winner:"Packers ", winScore:33,
    loser:"Raiders (Oakland)", losScore:14};
    jsData[2] = {bowl:"III", year:1969, winner:"Jets", winScore:16,
    loser:"Colts (Balto)", losScore:7};
    jsData[3] = {bowl:"IV", year:1970, winner:"Chiefs" , winScore:23,
    loser:"Vikings" , losScore:7};
    jsData[4] = {bowl:"V", year:1971, winner:"Colts (Balto)", winScore:16,
    loser:"Cowboys" , losScore:13};
    --!>
    >
    Now only interested in these data,I want to export it to some format
    that i like ,store somewhere else, How can I do this?
    a kind of csv in an hidden field to submit somewhere ?

    function xfer() {
    var report = document.forms[0].hiddenValues;
    for(var i in jsData) {
    report.value += i+';';
    for(var k in jsData[i]) report.value += jsData[i][k]+';';
    report.value += '\n';
    }
    // to see what happens
    document.getEle mentById('inf') .innerHTML = report.value;
    }


    <p><button onclick="xfer() ;">txfer</button></p>
    <form action="repport .php">
    <input type=hidden name="hiddenVal ues">
    <input type=submit>
    </form>
    <pre id="inf">
    </pre>
    I know inside javascript ,you just use it's variable name to access
    it ,but how about out side? just like we access html elements by it's
    tag name, can we just use some" xpath or DOM way" to access this
    array?
    var E = document.forms[0].hiddenValues.v alue.split('\n' );
    var txt = '<table border=1>', T='';
    for(var i=0, L = E.length; i<L; i++) {
    txt += '<tr>';
    T = E[i].split(';');
    for(var k=0, S = T.length; k<S; k++)
    txt += '<td>'+T[k]+'<\/td>';
    txt += '<\/tr>';
    }
    txt += '<\/table>';
    document.getEle mentById('resul t').innerHTML = txt;


    <div id="result"></div>


    or ... :

    <script type="text/javascript">
    var txt = '<table border=1><tr>';
    for(var k in jsData[0]) {
    txt += '<th>'+k+'<\/th>';
    }
    txt += '<\/tr>';
    for(var i in jsData) {
    txt += '<tr><th>'+i+'< \/th>';
    for(var k in jsData[i]) txt += '<td>'+jsData[i][k]+'<\/td>';
    txt += '<\tr>';
    }
    txt += '<\/table>';
    document.write( txt);
    </script>

    --
    sm

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: how to retrieve the data in array with html element access?

      Harry wrote:
      <script language="JavaS cript" type="text/javascript">
      <!--
      var jsData = new Array();
      jsData[0] = {bowl:"I", year:1967, winner:"Packers ", winScore:35,
      loser:"Chiefs", losScore:10};
      jsData[1] = {bowl:"II", year:1968, winner:"Packers ", winScore:33,
      loser:"Raiders (Oakland)", losScore:14};
      jsData[2] = {bowl:"III", year:1969, winner:"Jets", winScore:16,
      loser:"Colts (Balto)", losScore:7};
      jsData[3] = {bowl:"IV", year:1970, winner:"Chiefs" , winScore:23,
      loser:"Vikings" , losScore:7};
      jsData[4] = {bowl:"V", year:1971, winner:"Colts (Balto)", winScore:16,
      loser:"Cowboys" , losScore:13};
      var jsData = [
      {bowl:"I", year:1967, winner:"Packers ", winScore:35, ...},
      ...
      };
      --!>
      W3C's easy-to-use markup validation service, based on SGML and XML parsers.

      Now only interested in these data,I want to export it to some format
      that i like ,store somewhere else, How can I do this?
      >
      I know inside javascript ,you just use it's variable name to access
      it ,but how about out side? just like we access html elements by it's
      tag name, can we just use some" xpath or DOM way" to access this
      array?
      No.
      especially I'm using ruby(and Hpricot) to process some page content,
      now the choice for me is just use some regular expression to deal it
      like a dead string, not an array.....reall y appriciate if someone give
      me a hint
      Suppose you have control over the content, it would be easier to parse if
      you used initializers instead of constructors and several assigning
      statements (see above). JSON does this: <http://json.org/>

      But if you had control over the content, there would be no need for you to
      parse anything as you have the data already available in its raw form (that
      was the basis for the generated client-side code).

      So ISTM you are asking the wrong question, and, given that you are using
      Ruby (and Hpricot, whatever this is), you are probably also asking it in
      the wrong place.


      PointedEars
      --
      var bugRiddenCrashP ronePieceOfJunk = (
      navigator.userA gent.indexOf('M SIE 5') != -1
      && navigator.userA gent.indexOf('M ac') != -1
      ) // Plone, register_functi on.js:16

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: how to retrieve the data in array with html element access?

        Thomas 'PointedEars' Lahn wrote:
        Harry wrote:
        ><script language="JavaS cript" type="text/javascript">
        ><!--
        >var jsData = new Array();
        >jsData[0] = {bowl:"I", year:1967, winner:"Packers ", winScore:35,
        >loser:"Chiefs" , losScore:10};
        >jsData[1] = {bowl:"II", year:1968, winner:"Packers ", winScore:33,
        >loser:"Raide rs (Oakland)", losScore:14};
        >jsData[2] = {bowl:"III", year:1969, winner:"Jets", winScore:16,
        >loser:"Colts (Balto)", losScore:7};
        >jsData[3] = {bowl:"IV", year:1970, winner:"Chiefs" , winScore:23,
        >loser:"Vikings ", losScore:7};
        >jsData[4] = {bowl:"V", year:1971, winner:"Colts (Balto)", winScore:16,
        >loser:"Cowboys ", losScore:13};
        >
        var jsData = [
        {bowl:"I", year:1967, winner:"Packers ", winScore:35, ...},
        ...
        };
        Must be

        ];

        of course.


        PointedEars
        --
        var bugRiddenCrashP ronePieceOfJunk = (
        navigator.userA gent.indexOf('M SIE 5') != -1
        && navigator.userA gent.indexOf('M ac') != -1
        ) // Plone, register_functi on.js:16

        Comment

        • david.karr

          #5
          Re: how to retrieve the data in array with html element access?

          On Jun 1, 4:19 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
          wrote:
          Thomas 'PointedEars' Lahn wrote:
          Harry wrote:
          var jsData = [
          {bowl:"I", year:1967, winner:"Packers ", winScore:35, ...},
          ...
          };
          >
          Must be
          >
          ];
          >
          of course.
          Ironically, that is both the fix to the syntax error and your personal
          smiley :) .

          Comment

          Working...