1

I am working with the MVC4 application with the aid of an external js file. In the view (.cshtml) file, I have a function which performs an action of creating the row in the grid, based on the button click. I have defined the button click in the external .js file. But, when I tried to call the internal script function from that external js file method, it throws an exception saying that, that particular method is not defined.

I surfed but was not able to find a convincing answer..

Is what I'm trying possible??.. How should I achieve it??

Can any Js expert out there help me with this?...

Thanks All...;)

EDIT:

this is in external .js file:

    $('#AddRowButton').on('click', function () {
    window.CreateRow();
     }

in my view:(.cshtml)

    <script>
    function CreateRow()
    {
     // creting row goes here...
    }

    window.CreateRow = CreateRow; //defined like what @joseeight suggested...
    </script>
2
  • Is the "internal" function a global function? Is it defined before it is called? Can you show a cut-down version of your code here? Commented Jan 19, 2014 at 12:42
  • can you provide how you include external.js to your view? Commented Jan 19, 2014 at 16:57

3 Answers 3

2

This is most likely due to a scoping issue. The internal script and external must be in a different scopes. The easiest, and hackiest, way to get around this would be to add the internal method to the Window, and access it as such in the external.

//Internal script
function myInternalMethod () {
   //some code..
}
window.myInternalMethod = myInternalMethod;

Since window is global, and the name is the same, you could either use window.myInternalMethod or myInternalMethod when referencing it in the external scripts.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, still no luck...;(
Can you post some code? It's hard to see what is happening :)
0

Make sure your external file is included below the internal

 .......

    <script>

      function createRow(){
         console.log('created');
      }

    </script> 

  <script src = "external.js"></script>
</body>

Comments

0

yes it is possible only when you call that external js file from html where that internal javascript have..

Example : a.html

<html>
<head>
<script type="text/javascript">
    function displayAlert(){
        alert('displayAlert');
}
     </script>

    <script type="text/javascript" src="../../abc.js"></script>
  </head>
<body>
//call the sample() javascript function which is in abc.js
</body>
</html>

abc.js

function sample(){

displayAlert();

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.