3

I have a html code in which i had two javascripts. I want to call the function of 1 javascript into another. Below given is the structure of my javascript.

<html>
<head>
<script>
function a()
{
//function code
this.parent["asd"].b(); //problem in dis line 
}
</script>
<body>
<script>
var asd= new Asd();
function b()
{
//function code here
}
</script>
</body>
</html>

It works fine when i use it locally or from a web server. But if i upload this page in my facebook application, the parent class points somewhere else since its inside a iframe. How to resolve it. Is there any other way to call the function.

Praveen Singh
  • 534
  • 2
  • 8
  • 23

6 Answers6

4

You can:

=> combine both javascripts which you want to use in one

or

=> include a file first of which function you want to use in another javascript so it will automaticall get the value from that javascript file.

Trix
  • 587
  • 1
  • 6
  • 27
Jhanvi
  • 594
  • 3
  • 17
  • i am telling in genralized concept where we want to include two javascripts in our html or in view page...i think now i am making smwhat clear... – Jhanvi Sep 21 '12 at 06:26
  • order of javascript doesn't matter if it is global function. – Chamika Sandamal Sep 25 '12 at 06:28
  • yes you are correct in global scenario but it matters if you are going to use library functions or any other function defined in one file and want to call in another file... – Jhanvi Sep 25 '12 at 06:49
2

Its doesn't matter that you have written function in same script tag or in another it easily called with function name only like below

<html>
<head>
<script>
function a()
{
//function code
b(); //problem in dis line 
}
</script>
<body>
<script>
var asd= new Asd();
function b()
{
//function code here
}
</script>
</body>
</html>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
1

It is always better to combine all your Javascript files into one complete js file. So, you can just merge both your javascript files together. That would also save you from this trouble. You can also see this thread.

Community
  • 1
  • 1
xan
  • 4,640
  • 13
  • 50
  • 83
1

Can you try eval("asd").b(); instead of this.parent["asd"].b();

sureshunivers
  • 1,753
  • 12
  • 27
1

it doenst matter. you can call any function already refered in the HTML as in the same file. because ultimately everything in same page.

Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
0

Thank u all. i got the solution. i removed the function word from the javascript. now its working fine.

Praveen Singh
  • 534
  • 2
  • 8
  • 23