13 Replies - 1164 Views - Last Post: 13 January 2011 - 12:38 PM

#1 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

simple javascript?

Posted 12 January 2011 - 09:50 PM

My problem is this: print the sum of 2+2 and display the result in an alert window in the browser.
function Demo(){
var nsum = $(2+2=4);

alert(nsum.size());
}



How do I tell the query to solve the problem. I am very new to javascript so don't criticize too bad :) I am

Is This A Good Question/Topic? 0
  • +

Replies To: simple javascript?

#2 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: simple javascript?

Posted 12 January 2011 - 10:35 PM

So all you're trying to do is print the evaluation of 2+2?

What is the $ in there for? And what exactly are you trying to use the size() function to do?
Was This Post Helpful? 0
  • +
  • -

#3 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: simple javascript?

Posted 13 January 2011 - 06:29 AM

How about:


<html>
 <body>

  <script>
   //<!CDATA[
   var x = 2;
   var y = 2;
   var sum = x + y;

   alert(x + " plus " + y + " equals " + sum);

  //]]>
 </script>

 </body>
</html>



Better? and why is it now running when I pull it up in my browser?

This post has been edited by dbeaird727: 13 January 2011 - 06:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 256
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Re: simple javascript?

Posted 13 January 2011 - 06:44 AM

The reason it does it on your browser is because you did not specify an event for it to occur, so I believe it goes by default on document.ready but I could be wrong
Was This Post Helpful? 1
  • +
  • -

#5 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: simple javascript?

Posted 13 January 2011 - 09:06 AM

Try putting that into a function then calling that function when the page loads by adding onload = function_name() to the body tag.
Was This Post Helpful? 0
  • +
  • -

#6 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: simple javascript?

Posted 13 January 2011 - 10:23 AM

So Like this?

<html>
     <head>
     	  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          <title>javascriptadd</title>
     </head>
               <body>

                    <script type= "text/javascript"
                     //<![CDATA[
                  function_demo(){
                     var x = 2;
                     var y = 2;
                     var sum = x + y;

                     alert( x + " plus " + y +  " equals " + sum);
                  }
				  onload = function_demo();<--Semi-colon or no?
                    //]]>
                   </script>
              </body>
</html>


Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: simple javascript?

Posted 13 January 2011 - 10:51 AM

you can leave the CDATA part out unless you actually serve the document as XML.

Quote

onload = function_demo();<--Semi-colon or no?

semi-colon - yes/always
parentheses - no
better use window.onload = function_demo;

This post has been edited by Dormilich: 13 January 2011 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: simple javascript?

Posted 13 January 2011 - 10:58 AM

Still Nothing? Any Ideas? :(
Was This Post Helpful? 0
  • +
  • -

#9 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: simple javascript?

Posted 13 January 2011 - 11:02 AM

Is it possible you've disabled javascript in your browser? Also try checking the Javascript Error Console to see if any errors are coming up. Tools - Error Console in Firefox and Options button - Tools - Javascript Console in Chrome.

This post has been edited by Ntwiles: 13 January 2011 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#10 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: simple javascript?

Posted 13 January 2011 - 11:30 AM

It says there is a syntax error with the //![CDATA[ but I thought it was right
I took it out and it still didnt run. the html i have before it runs fine but no javascript. Does my cod look right>?
Was This Post Helpful? 0
  • +
  • -

#11 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: simple javascript?

Posted 13 January 2011 - 12:17 PM

And it says my function_demo isnt defined??

<html>
     <head>
     	  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          <title>javascriptadd</title>
     </head>
               <body>
                 <p>Hi! Check Out My page!</p>
                   <script type= "text/javascript">
                   //<![CDATA[
                     function_demo(){
                      var x = 2;
                      var y = 2;
                      var sum = x + y;

                      alert( x + " plus " + y +  " equals " + sum);
					  }
					 onload = function_demo;
                   //]]>
                   </script>
              </body>
</html>



Was This Post Helpful? 0
  • +
  • -

#12 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: simple javascript?

Posted 13 January 2011 - 12:28 PM

I could be wrong, but arent javascript functions declared like this?:

function function_demo(){

Was This Post Helpful? 1
  • +
  • -

#13 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 832
  • Joined: 25-October 08

Re: simple javascript?

Posted 13 January 2011 - 12:31 PM

Try this change:
<html>
     <head>
     	  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          <title>javascriptadd</title>
     </head>
               <body>
                 <p>Hi! Check Out My page!</p>
                   <script type= "text/javascript">
                   //<![CDATA[
                     function demo(){
                      var x = 2;
                      var y = 2;
                      var sum = x + y;

                      alert( x + " plus " + y +  " equals " + sum);
					  }
					 onload = demo;
                   //]]>
                   </script>
              </body>
</html>




This post has been edited by JMRKER: 13 January 2011 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#14 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: simple javascript?

Posted 13 January 2011 - 12:38 PM

Yes you do need to define the function as
 
function function_demo(){}



Thank You a ton!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1