12 Replies - 655 Views - Last Post: 25 June 2015 - 09:15 AM

#1 javanoob17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 86
  • Joined: 10-July 14

undefined error in my code after the contents of my function

Posted 24 June 2015 - 11:33 AM

Hello,

I want to thank all that will help me with my related issue. I am working on becoming a front end developer. When i load my function, i get an undefined after the contents of my function. I will add the source code to the bottom.


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title></title>
  <script type="text/javascript" src="variable scope.js"></script>
  <link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
	<button onclick="document.write(test());">Try it</button>
</body>
</html>



var exampleOne = "Hello";

function test(){
	
	var exampleTwo = "Hello Again";
	document.write(exampleOne + "<br/>" + exampleTwo);
	
	
}


Thank you all for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: undefined error in my code after the contents of my function

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5074
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 11:42 AM

There is really no reason to use document.write these days. I also don't think it makes sense in the context of a click event. I think it may have an issue with the fact that it is using document.write. Try substitute document.write with the "alert" function and see what you get.
Was This Post Helpful? 0
  • +
  • -

#3 javanoob17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 86
  • Joined: 10-July 14

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 11:47 AM

Thank you for your help. I am just learning the concepts. I am not thinking too much about logic at this point. When i did the document.write to alert, i get a undefined.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5074
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 11:50 AM

Make sure you don't have a space in your .js file either. Like all filenames, you really shouldn't use spaces. It might not see your function in the js file because it can't find the JS file since it has a space
Was This Post Helpful? 0
  • +
  • -

#5 javanoob17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 86
  • Joined: 10-July 14

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 11:57 AM

I tried the console.log and i dont get the an undefined.

i used document.close() to stop the browser from spinning.

It seems that it had to do with the document.write and the alert to display the message.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,899
  • Joined: 12-June 08

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 12:05 PM

Not to mention document.write tends to overwrite the entire page.. If I were you I would see what 'document.write' return value is.
Was This Post Helpful? 0
  • +
  • -

#7 javanoob17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 86
  • Joined: 10-July 14

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 12:14 PM

Cool thanks again.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,058
  • Joined: 08-June 10

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 12:27 PM

a little more explanation to what document.write() does:

https://developer.mozilla.org/en-US/docs/Web/API/Document/write said:

Note: as document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls document.open which will clear the document.


this behaviour is known since document.write() was invented.

This post has been edited by Dormilich: 24 June 2015 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1830
  • View blog
  • Posts: 5,761
  • Joined: 15-January 14

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 12:30 PM

The undefined issue is because you're trying to print the return value of your function:

document.write(test());

But, that function doesn't return a value, so the value is undefined. It's not an error, you're just telling it to print something that is not defined.
Was This Post Helpful? 0
  • +
  • -

#10 javanoob17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 86
  • Joined: 10-July 14

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 04:43 PM

Cool i get it now. So do i just need to put something in () and it will print out.
Was This Post Helpful? 0
  • +
  • -

#11 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1830
  • View blog
  • Posts: 5,761
  • Joined: 15-January 14

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 04:50 PM

Functions only do what you tell them to. If you pass a parameter to the function then you can write the code to do with that parameter whatever you want it to do. It's not looking for or printing a parameter right now so, no, just adding a parameter is not going to cause it to be printed, and you'll still see undefined because the function still isn't returning a value. There's a very basic introduction to functions in Javascript here:

http://www.w3schools...s_functions.asp
Was This Post Helpful? 0
  • +
  • -

#12 javanoob17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 86
  • Joined: 10-July 14

Re: undefined error in my code after the contents of my function

Posted 24 June 2015 - 06:21 PM

So do i need to reference an element and return with this: document.getElementById("element").innerHTML = test;
Was This Post Helpful? 0
  • +
  • -

#13 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1830
  • View blog
  • Posts: 5,761
  • Joined: 15-January 14

Re: undefined error in my code after the contents of my function

Posted 25 June 2015 - 09:15 AM

I'm not sure what you "need" to do, I don't know what your goal is. If you're just trying to get output the easiest way is to use console.log statements and then check the browser's console for the output. If you want to change the content of an element then, yes, you would get a reference to the element and set innerHTML.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1