/    Sign up×
Community /Pin to ProfileBookmark

struggling with global parameters

Please offer advice regarding the use of global parameters or alternative method(s) in the following javascript&html. The parameters passed in an onclick call are behaving as values of local variables rather than as intended values of the global variables. Thanks in advance for any suggestions.

  • 1. onclick with parameters calls global function page with parameters
  • [I]<!– page content –>
    <div class=”content img”><ul class=”none”>
    <li><img alt=”1394-80,95,18,93″ title=”1394-80,95,18,93″ height=”200px” width=”200px” src=”http://2008-collection.littledane.com/small/basics/split/1394_80_95_18_93 150x200cm.jpg” [B]onclick=”page(‘1394′,’80’)”[/B] /></li>
    <li>1394-80,95,18,93</li></ul></div>[/I]

  • 2. Alert shows global variables stylenum and colornum with parameters (‘1394′,’80’) passed in call and

  • 3. page.html opens pausing at window.onload = winonload; with alert showing stylenum and colornum with null values. My expectation was that the variables would have the values (‘1394′,’80’).
  • external .js file containing global functions and parameters:
    [I]/*— launch detail page from onclick —*/
    var stylenum = “”;
    var colornum = “”;
    var page = function(stylenum,colornum)
    {
    alert(stylenum + ” and ” + colornum);
    window.location.assign(“http://…page.html”);
    }

    var winonload =function()
    {
    window.onload = alert(stylenum + ” and ” + colornum);
    anotherfunction = function(stylenum, colornum){…};
    }
    [/I]

    page.html
    [I]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
    http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
    <html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
    <head profile=”http://dublincore.org/documents/dcq-html/“>
    <title>The Little Dane, Inc. annotated images of throws</title>
    <meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″/>
    <meta http-equiv=”Content-Script-Type” content=”application/javascript”>
    <meta http-equiv=”Content-Style-Type” content=”text/css”>

    <link rel=”stylesheet” type=”text/css” href=”mainstylesheet.css” />

    <script src=”pipesaccess.js” type=”text/javascript”></script>
    <script type=”text/javascript”>
    [B]window.onload = winonload;[/B]
    </script>
    </head>

    <body>

    <!– page header –>
    <div class=”headerbox leftheaderbox”>
    <img alt=”…” title=”…” src=”…” align=”left” hspace=”2″ /></div>

    <!– page content –>

    </body>
    </html>[/I]

    to post a comment
    JavaScript

    3 Comments(s)

    Copy linkTweet thisAlerts:
    @felgallMar 05.2008 — window.onload = alert(stylenum + " and " + colornum);

    runs the alert first and attaches the result to the onload which since it isn't a function can do nothing.

    To run it at the expected time use:

    window.onload = Function('alert(stylenum + " and " + colornum);');
    Copy linkTweet thisAlerts:
    @wood337authorMar 05.2008 — Thanks for responding, felgall. Unfortunately, both

    1. window.onload = alert(stylenum + " and " + colornum); and

    2. window.onload = Function('alert(stylenum + " and " + colornum);');

    produce the same result which is an alert box with the word "and" on the second line.

    Interestingly, the correct results are produced by specifying values in the variables, ie.

    [I]var stylenum = "1453";

    var colornum = "17";[/I]


    I am still puzzled why the global variables are not set by the function

    [I]var page = function(stylenum,colornum)

    {

    alert(stylenum + " and " + colornum);

    window.location.assign("http://...page.html");

    }[/I]
    Copy linkTweet thisAlerts:
    @NedalsMar 05.2008 — The variables parsed into the function are LOCAL to the function

    To set the GLOBAL variables you will need...
    <i>
    </i>var stylenum = "";
    var colornum = "";
    var page = function(varA,varB) {
    stylenum = varA;
    colornum = varB;
    ...
    }
    ×

    Success!

    Help @wood337 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.8,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...