0

I have this function that not work in another .js file

$('#Button1').click(function () {
var channelId = '55';
var matn = 'some';
   alert('s');
    $.ajax({
          type: 'POST',
          url: 'savedata.aspx/insertdata',
          async: false,
          data: "{'channelId':'" + channelId + "','matn':'" + matn + "'}",
          contentType: 'application/json; charset =utf-8',
          success: function (data) {
                   var obj = data.d;
                   if (obj == 'true')
                         alert("Data Saved Successfully");
                   },
                   error: function (result) {
                         alert("Error Occured, Try Again");
                   }
                });
           })

When I write it in html file it works properly but when I put it in another .js file it doesn't work here is head of my html

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>  

and i put this inside of my body

 <script src="gh.js" type="text/javascript"></script>
  • You need to wrap the JS code that you put in the `gh.js` file within a document.ready event handler – Rory McCrossan Nov 27 '17 at 13:45
  • Also note that jQuery 1.8.2 is a little outdated now. I'd suggest upgrading to at least 1.12 if you need IE support. 3.x if you don't. – Rory McCrossan Nov 27 '17 at 13:46
  • it works inside html with no problem. why not work in another .js? – ghane nikraftar Nov 27 '17 at 13:52
  • It all depends on where you put the script. If it's in the head you need to add document.ready. Also note that you should remove `async: false` as it's terrible practice. The browser will be giving you warnings about its use if you check the console – Rory McCrossan Nov 27 '17 at 13:56

0 Answers0