Button onClick event and Href-Location

2019-08-21 10:03发布

问题:

I used a solution given by "Chrules" found in this discussion:buttons with no href, onclick etc

like this :

<button id="clickable" >Click Me!!</button>
 ......
$("#clickable").click(function(){
 // Send user to this link
   insertRecord(); //SQLite Code
  location.href = "game.html";
   // location.href = "http://www.takemehere.com"; });

So When i used the location.href like he putted it (= "http://www.takemehere.com";) The insertRecord() method is done with success (the object is added to the database)

But when I use the location.href = "game.html"; the game.html page is opened but the insertRecord() method is not done i tried to put the full path of game.html but the same problem persist Have you any idea please ? Thank u in advance

回答1:

I think insertMethod() calls some async functions to do database updating. So you should use a callback before doing anything else.

Something like this (i don't know what is inside of insertRecord method);

function insertRecord(callback) {
  // do all sorts of database job
  // when it's finished
  // fire your callback
  callback();
}

insertRecord(function() {
  location.href = "page.html";
});

EDIT: After your comment, i see you already have defined a function to run after sql which is loadAndReset(). So simply put location.href = "page.html" in that function, and it should work.