javascript - Call a js function after reloading page - Stack Overflow

I have js following code. createNote() function is called when I click a "Crate Note" button

I have js following code. createNote() function is called when I click a "Crate Note" button on webpage

function createNote() {
   alert( 'page created' );        // for example note is created here
   window.location = document.URL; // refresh page to show new added note
   showEditNotePopup(); 
}

function showEditNotePopup() { 
  alert( 'show note edit page' );  // for example edit note popup shown here
}

Above code is working and creating note and also refreshing page using window.location = document.URL;. But after reloading page it is not calling showEditNotePopup() function. Is there any way to achieve this without using AJAX.

I have js following code. createNote() function is called when I click a "Crate Note" button on webpage

function createNote() {
   alert( 'page created' );        // for example note is created here
   window.location = document.URL; // refresh page to show new added note
   showEditNotePopup(); 
}

function showEditNotePopup() { 
  alert( 'show note edit page' );  // for example edit note popup shown here
}

Above code is working and creating note and also refreshing page using window.location = document.URL;. But after reloading page it is not calling showEditNotePopup() function. Is there any way to achieve this without using AJAX.

Share Improve this question asked Dec 3, 2011 at 11:59 StudentStudent 1,8639 gold badges37 silver badges50 bronze badges 1
  • for refreshing use just: location.reload() – mgraph Commented Dec 3, 2011 at 12:07
Add a ment  | 

1 Answer 1

Reset to default 5

What happens here, is that, you are sending a pletely new HTTP Request to the server using window.location. Thus, it's not possible to do that. However, there are some workarounds. I'll write them here:

One way is to send another parameter to the server on window.location. For example:

window.location = document.URL + "?popup=true";

Now, on the server, check this parameter. If it exist, then create a self-invoked function to show the newly created note's info.

Another approach is to use ajax, instead of full request. This way, your function would be:

function createNote() {
   alert( 'page created' );        
   // use ajax to store new page, and update the UI accordingly
   showEditNotePopup(); 
}

Now, showEditNotePopup(); works like a charm.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745272992a4619863.html

相关推荐

  • javascript - Call a js function after reloading page - Stack Overflow

    I have js following code. createNote() function is called when I click a "Crate Note" button

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信