Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I've been playing around w/ this

Status
Not open for further replies.

mmt4331

Programmer
Dec 18, 2000
125
0
0
US

I've been playing around w/ this code for a couple hours...whenever I try to run it on the IE explorer, it shows nothing. What is wrong?

<html>

<title>Javascript for Dummies</title>

<head>

<script language=&quot;javascript&quot;>

function showme(mvs,unix) {

if (mvs == &quot;OS/390&quot;) {
document.writeln(&quot;MVS is &quot; + mvs)
}
else if (mvs == &quot;OS/380&quot;) {
document.writeln(&quot;Unix is &quot; + unix)
}}

function Shop(mvs,unix,web) {
this.mvs = mvs
this.unix = unix
this.web = web
this.reporting = showme(mvs,unix)
}

</script>

</head>
<body>
<br>
<script language=&quot;javascript>

mark = new Shop(&quot;OS/390&quot;, &quot;Linux&quot;, &quot;Navigator&quot;)
mark.reporting

susan = new Shop(&quot;ZOS&quot;, &quot;RedHat&quot;, &quot;IE&quot;)
susan.reporting

</script>
</body>
</html>
 
You should call methods like a function with ()

mark = new Shop(&quot;OS/390&quot;, &quot;Linux&quot;, &quot;Navigator&quot;)
mark.reporting()

susan = new Shop(&quot;ZOS&quot;, &quot;RedHat&quot;, &quot;IE&quot;)
susan.reporting()

Good luck and have fun! :)

Gary Haran
********************************
 

I put the () on the end of the methods and it still doens't work. Any more suggestions? Thx.
 

you have the clossing double quote missing in the second javascript

<script language=&quot;javascript>
mark = new Shop(&quot;OS/390&quot;, &quot;Linux&quot;, &quot;Navigator&quot;)
mark.reporting

susan = new Shop(&quot;ZOS&quot;, &quot;RedHat&quot;, &quot;IE&quot;)
susan.reporting

</script>
 

Oh Duh! Thanks for pointing that out. It works now.
 
Something is weird here. You should use this.propertyName to reference items here for simplicity.

I prefer using anonymous functions to create methods rather than link to them using links and like to keep everything into collections (or arrays) so I can use them later on with a loop or something.

Here is how I would write things :

Code:
<html>
  <head>
    <title>Javascript for Intelligent people like ourselve :-)</title>

    <script language=&quot;javascript&quot;>

    // store all shops here so you can loop through them easily
    var shops = new Array();

    // upgrade the Array object so you can call a function on all items contained in it
    Array.prototype.report = function()
    {
      var retString = &quot;&quot;
      for (var i = 0; i < this.length; i++)
      {
        retString += this[i].report()
      }
      return retString
    }


    function Shop(userName, os, osVersion, userAgent)
    {
      this.userName  = userName
      this.os        = os
      this.osVersion = osVersion
      this.userAgent = userAgent

      this.report = function ()
      {
        return this.userName + &quot; is using &quot; + this.userAgent + &quot; on &quot; + this.os + &quot; &quot; + this.osVersion + &quot;<br>&quot;
      }

      // add this item to the shops collection
      shops.push(this)
    }

    </script>

    </head>
    <body>
  </head>
  <body>


    <script language=&quot;javascript&quot;>

    new Shop(&quot;Mark&quot;,  &quot;OS/390&quot;, &quot;Linux&quot;,  &quot;Navigator&quot;)
    new Shop(&quot;Susan&quot;, &quot;ZOS&quot;,    &quot;RedHat&quot;, &quot;IE&quot;)
    new Shop(&quot;Gary&quot;,  &quot;RH9.0&quot;,  &quot;RedHat&quot;, &quot;Mozilla Firebird&quot;)

    // show all items in the shops collection
    document.write(shops.report())

    </script>


  </body>
</html>

Anyhow it's just an example. You don't have to use this but it works great for me.

Gary Haran
********************************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top