JavaScript Match string with clicked URL on unload of current page match string with regex,match string with spaces regex,match

I have a form with URL:

localhost:83/mysite/item/UpdateItems/225

I have a temporary table in which I have loaded data and display in my form from temporary table.

When I refresh the page I don't want to fire an ajax call and truncate the table. But if I click on another link from menu then I do want to fire the ajax call and truncate tables.

Is it possible?

I have used beforeunload for truncate table when click on another menu:

 $(window).on('beforeunload', function(){
     //My ajax code for truncate table
});

But this works on page refresh also, which I don't want. So I tried with matching string in URL:

$(window).on('beforeunload', function(e){
    if (document.URL.indexOf("UpdateItems") > -1) {

        console.log("page refresh");

     }else{
         console.log("not refreshed");  
      }
});

But this always go in first page refresh condition even if I have clicked on another menu.

Answer:1

Try this , it will definitely work,

    <SCRIPT language="javascript">

    window.onbeforeunload=before;

    function before(evt)
    {
       if(window.location.toString().indexOf("UpdateItems") > -1) { 

        console.log("page refresh");

       }
       else{
         console.log('Not');
       }
    }
    </script>
Answer:2

Here is the object I want to re-create dynamically var myPath = { one: [ new google.maps.LatLng(1,2), new google.maps.LatLng(3,4)) ]}; Here is my try without success: var ...

Here is the object I want to re-create dynamically var myPath = { one: [ new google.maps.LatLng(1,2), new google.maps.LatLng(3,4)) ]}; Here is my try without success: var ...

I'm working on a simple script for my site to upload images. I have a multiple file input <input type = 'file' name = 'files[]' id = 'hiddenFile' multiple> that is being triggered by a div click....

I'm working on a simple script for my site to upload images. I have a multiple file input <input type = 'file' name = 'files[]' id = 'hiddenFile' multiple> that is being triggered by a div click....

  1. javascript filereader file name
  2. javascript filereader get name
  3. javascript filereader get file name

I am working on some pretty old code and in every jsp file where there is a javaScript block I see some bizarre Syntax. It goes like : <script language="JavaScript"> <!-- here is the ...

I am working on some pretty old code and in every jsp file where there is a javaScript block I see some bizarre Syntax. It goes like : <script language="JavaScript"> <!-- here is the ...

I been trying to do something like this, where clicking on the list links to a different address("#/tab/cases/case-detail/{case.ID}") and clicking on the play button goes to another address("#/tab/...

I been trying to do something like this, where clicking on the list links to a different address("#/tab/cases/case-detail/{case.ID}") and clicking on the play button goes to another address("#/tab/...

  1. angular repeat item