-2

how can I execute a java script after page load is complete

<script src="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=503589&bid=1747907" type="text/javascript"></script>

Thank you guys

Mohamed Fadli
  • 9
  • 1
  • 1
  • 2

6 Answers6

1

If you use jquery, the below code might work:

$(document).ready(function() {
    $.getScript("http://bdv.bidvertiser.com/BidVertiser.dbm?pid=503589&bid=1747907");
});

Wihtout Jquery

window.onload = function() {
    var element = document.createElement("script");
    element.src = "http://bdv.bidvertiser.com/BidVertiser.dbm?pid=503589&bid=1747907";
    document.getElementsByTagName("head")[0].appendChild(element );
}
Gopikrishnan
  • 179
  • 6
  • shown in console : Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened. – Mohamed Fadli Jan 30 '16 at 16:33
0

This will run some javascript when the script file is loaded:

<script src="mysrc.js" onload="foo"></script>
<script>
  function foo() {
    console.log('Loaded script');
  }
</script>

This will run javascript when the entire page is loaded:

document.addEventListener('DOMContentLoaded', function(){
  console.log('Loaded page');
});
sg.cc
  • 1,546
  • 16
  • 37
0

Append the script load on ready event:

$(function(){
  var script = document.createElement("script");
  script.setAttribute("type", "text/javascript");
  script.setAttribute("src", "http://bdv.bidvertiser.com/BidVertiser.dbm?pid=503589&bid=1747907");
  document.body.appendChild(script);
});

Here is a jsfiddle: https://jsfiddle.net/ndeLhkw9/

Arman Bimatov
  • 1,561
  • 4
  • 21
  • 31
0

You could try jQuery deferred.done () , see more deferred.done().

Ci Kai
  • 325
  • 1
  • 4
  • 15
0
$( document ).ready(function() {

$("head").append('<script type="text/javascript" src="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=503589&bid=1747907"></script>');
});
Nofi
  • 1,647
  • 11
  • 18
0

Here's an example using jQuery:

function require(script) {
    $.ajax({
        url: script,
        dataType: "script",
        async: false,           // <-- This is the key
        success: function () {
            // all good...
        },
        error: function () {
            throw new Error("Could not load script " + script);
        }
    });
}

You can then use it in your code as you'd usually use an include:

require("http://bdv.bidvertiser.com/BidVertiser.dbm?pid=503589&bid=1747907");
Nofi
  • 1,647
  • 11
  • 18