Solved

Need to run ajax when closing a browser tab in juery

Posted on 2016-07-25
1
54 Views
Last Modified: 2016-07-26
We have tried to insert the record using ajax when if user closing the browser tab. But the code i used didn't working. Please check below.
$(window).unload(function() 

	 var currentTime = new Date();
                var DurationInSeconds = (currentTime - PhotoLoadTime) / 1000;
                $.ajax({
                	 async: false,
                    url: "/property/SavePhotoDuration?propertyid=" + propertyid + "&imageid=" + imageid + "&seconds=" + DurationInSeconds
                }).done(function (rsp1) { });

    });

Open in new window


And here the full code for your reference,
var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};
var PhotoLoadTime = null;
var lastimageid;
var DurationInSeconds;

function setThumbnails() {
    
    var propertyid = unescape(getUrlParameter('propertyid'));
    $.ajax({
        url: "/home/GetPropertyData?propertyid=" + propertyid
    }).done(function (rsp) {
        var propertyData = JSON.parse(rsp);

        for (var i = 0; i < propertyData.length; i++) {
            var imageSrc = "" + propertyData[i]['imageThumb'];
            var fullImage = "" + propertyData[i]['image'];
            var address = "" + propertyData[i]['Address'];
            var description = "" + propertyData[i]['description'];
            var imageid = "" + propertyData[i]['imageid'];
            var TextColor = "" + propertyData[i]['TextColor'];
            $("#tn-list").append('<div data-color="' + TextColor + '" data-imageid="' + imageid + '" id="' + "image-" + i + '" data-image="' + fullImage + '" data-address="' + address + '" data-description="' + description + '" class="thumb"><img src=' + imageSrc + ' title="' + description + '"  /></div>');
            lastimageid = propertyData[0]['imageid'];
        }

        $(".thumb").click(function () {
            var imageid = lastimageid;
            lastimageid = $(this).data("imageid");
            if (PhotoLoadTime != null) {
                var currentTime = new Date();
                var DurationInSeconds = (currentTime - PhotoLoadTime) / 1000;
                $.ajax({
                    url: "/property/SavePhotoDuration?propertyid=" + propertyid + "&imageid=" + imageid + "&seconds=" + DurationInSeconds
                }).done(function (rsp1) { });
            }
            PhotoLoadTime = new Date();
           
            var image = $(this).data("image");
            var address = $(this).data("address");
            var description = $(this).data("description");
            var TextColor = $(this).data("color");
           
            document.getElementById('innerIframe').src = document.getElementById('innerIframe').src + "&imgSelected=" + image + "&address=" + address + "&TextColor=" + TextColor + "&description=" + description;
        });
        setIframe();



        $("#image-0").click();
    });

   
}

$(document).ready(function () {
    setThumbnails();

$(window).unload(function() 

	 var currentTime = new Date();
                var DurationInSeconds = (currentTime - PhotoLoadTime) / 1000;
                $.ajax({
                	 async: false,
                    url: "/property/SavePhotoDuration?propertyid=" + propertyid + "&imageid=" + imageid + "&seconds=" + DurationInSeconds
                }).done(function (rsp1) { });

    });


 
});

function setIframe() {
    
    var iframeUrl = parent.document.getElementById('panorama').src.replace("pannellum.htm", "pannellumInner.htm");
    document.getElementById('innerIframe').src = iframeUrl;

}

Open in new window

0
Comment
Question by:karthik80c
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41728835
This works for me
$(function() {
  $(window).on('beforeunload', function() {
    $.ajax({
      async: false,
      type: 'POST',
      url: 't1447.php',
      data: $('#dataform').serialize()
    });
  });
});

Open in new window

Working sample here
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question