0

i wanna know can ajax call triggering onload event on the targeted page? so it's like this, i have one page (test.html) with simple function to change the content of a div that will run when the page load... here is the code :

<body onLoad = "a()">
    <div id="main">the result is here</div>
</body>
<script>
    function a()
    {
        document.getElementById("main").innerHTML =
        "Success";
    }
</script>

and i have another page (call.html) with ajax call targeted test.html and show the result inside the div... here is the code :

<body>
    <button onclick="call()">Click</button>
    <div id="box"></div>
</body>
<script>
    function call()
{
    var xmlhttp = new XMLHttpRequest();
    var url = "test.html";

    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("box").innerHTML =
            xmlhttp.responseText;

            alert("Success");

        }
    }
    xmlhttp.send();
}
</script>

if i just simply load the test.html, the content inside div will change, but if i use call.html to call that page, the inside won't change...

is this because ajax doesn't trigger function inside onload event?

van32
  • 28
  • 6

1 Answers1

0

This is happening because you are trying to open a URL from local i.e. using file:// and not via HTTP or HTTPS.

This thread can give you a better insight on how to proceed further.

Community
  • 1
  • 1
CS23
  • 16
  • 1
  • thanks for the quick reply... i already read the thread u gave me and i try google flag but it doesn't work, since u said i need to open url via http or https, can u give me some explaination about that? do i need to make some changes in my code? – van32 Nov 28 '14 at 22:00
  • Please try hosting the files on a webserver (e.g. tomcat) then using this same code you should be able to load the content over ajax – CS23 Dec 08 '14 at 18:42
  • do i really need to hosting the files on a website? can't i do that locally? since i have xampp installed on my computer... if that's the case, i'll try hosting it and let u know if it's work or not... – van32 Dec 13 '14 at 12:20