1

I have a js file.

File Name: propoties.js

function simple()
{
    alert("simple");
    var text = "Control";
}

These is my html code. What i want is alert that text variable in html.

<html>
<script type='text/javascript' src='path/propoties.js'></script>    
<script>
    simple();
    alert(text); /* It is not working */
</script>
 </html>

Please help me these. Thank you.

  • 5
    The `text` variable is declared within the `simple()` function. I suggest you read about Javascript variable scope: http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Rory McCrossan Jul 03 '15 at 08:23

4 Answers4

6

Your js file:

var simple=function(){
   var textMultiple = {
        text1:"text1",
        text2:"text2"
    };
   return textMultiple;
}

In your html:

<html>
    <script type='text/javascript' src='./relative/path/to/propoties.js'></script>    
    <script>
      alert(simple().text1);
      alert(simple().text2);
    </script>
 </html>

here is a plunkr demo.

Pravin
  • 1,671
  • 5
  • 23
  • 36
  • It's multiple variables. How to call? –  Jul 03 '15 at 08:58
  • Thanks for your answer. i gave the up vote. Thanks for your help. –  Jul 03 '15 at 09:26
  • i am glad it helped... dont forget to vote up answers that helped and select the answer by clicking the tick below it. and if you want to stick to javascript, you should learn its scope. [here](http://javascriptissexy.com/javascript-variable-scope-and-hoisting-explained/) is good article for it. – Pravin Jul 03 '15 at 09:28
1

If you want to alert the text in external file you need to declare the variable as global like below.

var text = "Control";
function simple()
{
    text="Changed";
    alert("simple");   
}

or you can declare the variable using window keyword

function simple()
{
    alert("simple");   
    window.text = "Control";
}

please check in plunker http://plnkr.co/edit/HjkwlcnkPwJZ7yyo55q6?p=preview

Vipin
  • 847
  • 1
  • 10
  • 21
  • please alert as alert(window.text ) after calling simple() function – Vipin Jul 03 '15 at 08:31
  • First you make sure whether the include path of external js file is correct. – Vipin Jul 03 '15 at 08:35
  • It shows simple alert. Path of external js file is correct. –  Jul 03 '15 at 08:43
  • So, both of my code will work, I have checked in my system – Vipin Jul 03 '15 at 08:45
  • Can you provide any jsfiddle please? Still it's not working. –  Jul 03 '15 at 08:54
  • you should avoid adding variables to the global window object as much as possible. encapsulation is encouraged. declare variables inside function scope, return it and access it only when it is required. – Pravin Jul 03 '15 at 09:07
  • Thanks for your answer. i gave the up vote. Thanks for your help. –  Jul 03 '15 at 09:26
  • I'm glad to hear that you got the answer :) – Vipin Jul 03 '15 at 09:34
  • I gave two tick mark to you. Because you answered the question very fast. Thank for wonderful job. –  Jul 03 '15 at 09:36
1

like you did it the "text" variable is only set in the scope of the function "simple" .

you should make the "text" variable global by declaring it outside the function.

var text = "";

function simple()
{
    alert("simple");
    text = "Control";
}
Federico
  • 1,231
  • 9
  • 13
  • sorry but it is working. there may be another error somewhere else in the code .... do you get any other JavaScript errors ? – Federico Jul 03 '15 at 08:36
  • Can you provide any jsfiddle please? Still it's not working. –  Jul 03 '15 at 08:53
  • Thanks for your answer. i gave the up vote. Thanks for your help. –  Jul 03 '15 at 09:26
0

Declaring the variable globally will work. ie

var text = "";
function simple()
{
     text = "Control";
}

see plunker

I'm nidhin
  • 2,592
  • 6
  • 36
  • 62
  • Its too late the answered that question. Thanks for your answer, I gave the tick you Thank you. –  Jul 03 '15 at 10:09