Search Results

Search found 2 results on 1 pages for 'agmin'.

Page 1/1 | 1 

  • How to reliably send a request cross domain and cross browser on page unload

    - by Agmin
    I have javascript code that's loaded by 3rd parties. The javascript keeps track of a number of metrics, and when a user exits the page I'd like to send the metrics back to my server. Due to XSS checks in some browsers, like IE, I cannot do a simple jquery.ajax() call. Instead, I'm appending an image src to the page with jquery. Here's the code, cased by browser: function record_metrics() { //Arbitrary code execution here to set test_url $esajquery('#MainDiv').append("<img src='" + test_url + "' />"); } if ($esajquery.browser.msie) { window.onbeforeunload = function() { record_metrics(); } } else { $esajquery(window).unload( function(){ record_metrics(); } ); } FF aborts the request to "test_url" if I use window.onbeforeunload, and IE8 doesn't work with jquery's unload(). IE8 also fails to work if the arbitrary test_url setting code is too long, although IE8 seems to work fine if the is immediately appended to the DOM. Is there a better way to solve this issue? Unfortunately this really needs to execute when a user leaves the page.

    Read the article

  • How can I append and execute a script inside an iframe in a cross browser way (specifically IE!)?

    - by agmin
    Background: I need to load ad scripts after the DOM has loaded. Because many of the scripts use document.write() and other potentially bad functions to run after the DOM has loaded, I want to load the scripts inside an iframe. So when the ad needs to be shown, an event is triggered which does the following: var iframe = document.createElement('iframe'); iframe.setAttribute('id', 'iframeId'); iframe.setAttribute('src', 'about:blank'); var adContainer = document.getElementById('AdContainer'); adContainer.appendChild(iframe); var val = '<html><head></head><body>'; val += '<scr' + 'ipt type="text/javascript" src="' + url + '"></scr' + 'ipt>'; val += '</body></html>'; If I don't assign the html and body tags to val, the script automatically gets appended to the head of the iframe and fails to execute in FF. In IE, the script doesn't execute with or without the head/body/html tags. var doc = iframe.contentWindow || iframe.contentDocument; if (doc.document){ doc = doc.document } doc.open(); doc.write(val); doc.close(); I found this last bit of code here: http://stackoverflow.com/questions/1591135/why-does-appending-a-script-to-a-dynamically-created-iframe-seem-to-run-the-s. I do NOT want to load jquery in the iframe though, I just want to append a script and have it execute. My current solution seems to work great in FF and Webkit. However, IE doesn't execute the script. The script is written to the page, but doesn't start running. Is there a better way to append the script to the iframe so it will run cross browser? Or is it possible to tell IE to run the script? I know that I could load an external document with the ad script via the iframe, but I don't want to make the extra call to my server if I can do this dynamically. Also, I've tried using appendChild() on the iframe's body to insert the script, but since the script element is created outside the iframe's DOM this doesn't seem to work.

    Read the article

1