0

This is my code on the razor view engine

<a href="#" onclick="Delete('@recordID', '@pID');" id="@pID.ToString()">Delete</a>

I have an external js file which contains the code for function detele:

alert('external');

function Delete(recordID, pID) {
alert('function called');
}

I added alert('external'); to check if I have the correct reference to the external js file, and it worked. Not sure why it keeps returning js error: Microsoft JScript runtime error: 'Delete' is undefined

tereško
  • 58,060
  • 25
  • 98
  • 150
NKD
  • 1,039
  • 1
  • 13
  • 24
  • 1
    Use a browser with good JS debugging, I usually run Firefox which gives me good error messages. Usually, there is a syntax error somewhere which stops the parsing of the script. – Atle Oct 14 '13 at 18:59
  • 1
    is the external file loaded to the page? – Dvir Oct 14 '13 at 19:01
  • yes, it is loaded. I use Chrome for debugging js. I see the alert "external". – NKD Oct 14 '13 at 19:03
  • I ran your code in IE7, it worked. – Atle Oct 14 '13 at 19:04
  • I use IE 9 and Chrome. This should be plain simple... not sure why i'm getting this error. I probably did something stupid... – NKD Oct 14 '13 at 19:05
  • Where do inlclude the script? Below the `` inside `` – Atle Oct 14 '13 at 19:05
  • Between, I also did another test by copied the Delete function code and put it directly on the razor view engine then it works. So I know it's not syntax error. Not sure why it's not working when i put it back on the external js file. – NKD Oct 14 '13 at 19:06
  • I include the script at the bottom of my razor view – NKD Oct 14 '13 at 19:08
  • @Scripts.Render("~/Scripts/external.js") – NKD Oct 14 '13 at 19:08
  • Chrome error message: Uncaught ReferenceError: Delete is not defined – NKD Oct 14 '13 at 19:11
  • Script isn't being processed in time, although it should be since it's at the bottom of the page, correct? – tymeJV Oct 14 '13 at 19:28
  • I managed to get this to work but it still doesn't make sense to me. Anyway, I had my Delete function inside of the document ready of the external.js file. As soon as I moved it (the Delete function) out side of the document ready of the external.js file. It works. I purposely put it in the document ready initially to make sure that it's available and it should work at least once... oh well. If anyone has other insight to this behavior. I would love to hear from you. And thank you everyone who has been from contributing to my question. – NKD Oct 14 '13 at 19:33
  • to tymeJV: as for the script being process in time... I thought putting it in document ready would some how ensure that the function is there when the document is fully loaded and ready... – NKD Oct 14 '13 at 19:35
  • If the function was defined in the document.ready, you wouldn't be able to call it later. You should only put functions in the document.ready if you are going to only call it from within there. This somewhat explains it: http://stackoverflow.com/questions/6780890/jquery-function-inside-document-ready-function – wilsjd Oct 14 '13 at 19:56
  • Thank you wilsjd. That is good to know and thanks for the link! – NKD Oct 14 '13 at 20:08

0 Answers0