Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    UGH NEED HELP..so stuck :(

    Alright well im doing a college project and were supposed to make a website.
    I have a site that takes input on one form and then opens another page.

    This other page has javascript code that disassembles the query string and assigns the values to variables. Basically I wanna know if there is a way to access a variable in the head tag from html in the body tag.

    EXAMPLE
    <SCRIPT LANGUAGE="JAVASCRIPT">
    <!--
    var fName="Scott";
    //-->
    </SCRIPT>
    <BODY>
    <h5> (someway to output Scott without typing "Scott"..like...document.fName) </h5>
    </BODY>

    I want to make a table in the body tag using HTML because using javascript overwrites the css rendering. I want that table to use the values from the previously declared variable. Please view the attachment and HELP.
    Thanks so much in advance.
    (I've spent like an hour researching this and can't find anything. Apparently when a variable is declared globally its scope includes the whole document...im guessing i could access it while in the body tags)
    SummaryPage.txt
    Last edited by aburningflame; 07-19-2007 at 11:51 PM.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    var in the head should survive into to body. you could add "window." before everything if in doubt.

    you could also plant this in your body; it will provide json of the querystring via it's args property:

    Code:
    function sQS() {
        var allem = [];
        var RxUrl = new RegExp(/^[^\?]+\??/);
        var RxQsSplit = new RegExp(/[;&]/);
        var RxValRep = new RegExp(/\+/g);
        var Params = {};
        var cItem = window.location.toString();
        var queryURL = cItem.replace(RxUrl, "");
        var Pairs = queryURL.split(RxQsSplit);
        var mx = Pairs.length;
        for (var i = 0; i < mx; i++) {
            var KeyVal = Pairs[i].split("=");
            if (!KeyVal || KeyVal.length != 2) {
                continue;
            }
            var key = unescape(KeyVal[0]);
            var val = unescape(KeyVal[1]);
            val = val.replace(RxValRep, " ");
            try {
                Params[key] = eval(val);
            } catch (ee) {
                Params[key] = val;
            }
        }
        var filecache = cItem;
        var srcInfo = {file:filecache, bootTime:new Date, urlSearch:queryURL, args:Params};
        return srcInfo;
    }
    
    //    this array can then be converted to a table:
    
    function obVals(ob) {
        var r = [];
        var i = 0;
        for (var z in ob) {
            if (ob.hasOwnProperty(z)) {
                r[i++] = ob[z];
            }
        }
        return r;
    }
    
    Array.prototype.toTR=function () {
            return "\n<tr class='autoTR' >\n" + wrapHT(this, "td") + "</tr>\n";
    }
    
    Array.prototype.group=function (f) {
        mx = this.length;
        var r = [];
        for (z = 0; z < mx; z++) {
            if (this[z]) {
                r[r.length] = f(this[z], this, z);
            }
        }
        return r;
    }
    
    function wrapHT(r, tg) {
        var tt = [];
        tt[0] = "<" + tg + ">";
        tt[3] = "</" + tg + ">\n";
        tt[2] = r.join(tt[3] + tt[0]);
        return tt.join("");
    }
    
    //  ------------------------
    
    //ex: url=xxx.com?a=1&b=2&c=3
    
    var qsOb= sQS().args //[{a:1},{b:2},{c:3}]
    res=qsOb.group(function(o){return obVals(o)}).toTR() /* returns:
    
    <tr class='autoTR' >
    <td>1</td>
    <td>2</td>
    <td>3</td>
    </tr>                    */

    Use CSS classes to preserve formatting.
    (in the head section)
    Code:
    <style>
        .autoTR {border: 2px solid windowframe; font-weight: bold; } 
    </style>
    Last edited by rnd me; 07-20-2007 at 06:25 AM.

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First of all Id like to say thanks for the response.
    Anyways I think planting that in my body is a little to advanced.
    but adding window. before everything seems a little easier. I still dont get how to implement this.

    Code:
    <HTML>
    <HEAD><TITLE>Summary</TITLE>
    
    <SCRIPT LANGUAGE="JAVASCRIPT">
    <!--
    var window.fName="";
    var window.lName="";
    var window.ccNo="";
    var window.exMonth="";
    var window.exYear="";
    var window.ccType="";
    var window.donAmount="";
    var window.extraction="";
    var window.query="";
    
    
    function load(){
       retrieve();
    }
    
    function retrieve(){
        window.query=window.location.search.substring(1);
        window.query=window.query+"&";
        
       
        window.fName=extract(query);
       window.lName=extract(query);
        window.ccNo=extract(query);
        window.exMonth=extract(query);
        window.exYear=extract(query);
        window.ccType=extract(query);
        window.donAmount=extract(query);
       
    }
    
    
    function extract(tempQuery){
       equalPos= tempQuery.indexOf("=");
       conPos = tempQuery.indexOf("&");
       extraction = tempQuery.substring(equalPos+1, conPos);
       query=tempQuery.substring(conPos+1);
       return extraction;
       
    }
    
    // -->
    </SCRIPT>
    
    
    <link rel="stylesheet" type="text/css" href="css/stylesheet.css">
    </HEAD>
    <BODY onLoad="load()">
      <TABLE BORDER="1">
      <TR>
      <TD> window.fName </TD> 
      </TR>
      </TABLE>
    </BODY>
    </HTML>
    Im really not sure if I can do what I wanna do this way.


  •  

    Posting Permissions

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