Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript does not work upon AJAX call
    primarykey
    data
    text
    <p>I have a page which does an AJAX call and loads an entire page. The page that gets loaded has some Javascript. The javascript works on page by itself when loaded, but when its gets loaded by AJAX, the Javascript does not work. I dont know what I am missing.</p> <p><strong>The code of the loaded page</strong></p> <pre><code>&lt;html&gt; &lt;script type="text/javascript"&gt; function showfield(name){ if(name=='lstbox')document.getElementById('div1').style.display="block"; else document.getElementById('div1').style.display="none"; } function hidefield() { document.getElementById('div1').style.display='none'; } &lt;/script&gt; &lt;head&gt; &lt;/head&gt; &lt;body onload="hidefield()"&gt; &lt;form action = "test2.php" method = "post"&gt; Please enter a name for the App &lt;input type = "text" name = "name"&gt; &lt;table border = "1"&gt;&lt;tr&gt;&lt;th&gt;Choose a Label&lt;/th&gt;&lt;th&gt;Choose an element&lt;/th&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;input type = "text" name = "label1" /&gt;&lt;/td&gt; &lt;td&gt; &lt;select name = "elementtype1" id="elementtype1" onchange="showfield(this.options[this.selectedIndex].value)"&gt; &lt;option value = 'select'&gt; Select &lt;/option&gt; &lt;option value='txtbox'&gt;Text Box&lt;/option&gt; &lt;option value='lstbox'&gt;List Box&lt;/option&gt; &lt;option value='chkbox'&gt;Check Box&lt;/option&gt; &lt;option value='radio'&gt;Radio Button&lt;/option&gt; &lt;/select&gt;&lt;/td&gt;&lt;td&gt;&lt;div id="div1"&gt;Enter Listbox options: &lt;input type="text" name="whatever1" /&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;input type = "submit" value = "Submit"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>The code of the loading(AJAX) page is</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;script src="ajax.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="responseHTML.js" type="text/javascript"&gt;&lt;/script&gt; &lt;div id="storage" style="display:none;"&gt; &lt;/div&gt; &lt;div id="displayed"&gt; &lt;FORM name="ajax" method="POST" action=""&gt; &lt;p&gt; &lt;INPUT type="BUTTON" value="Get the Panel" ONCLICK="loadWholePage('appcreator.php')"&gt; &lt;/p&gt; &lt;/FORM&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>The ajax.js code</strong></p> <pre><code>function createXHR() { var request = false; try { request = new ActiveXObject('Msxml2.XMLHTTP'); } catch (err2) { try { request = new ActiveXObject('Microsoft.XMLHTTP'); } catch (err3) { try { request = new XMLHttpRequest(); } catch (err1) { request = false; } } } return request; } </code></pre> <p><strong>The responseHTML.js code</strong></p> <pre><code>function getBody(content) { test = content.toLowerCase(); // to eliminate case sensitivity var x = test.indexOf("&lt;body"); if(x == -1) return ""; x = test.indexOf("&gt;", x); if(x == -1) return ""; var y = test.lastIndexOf("&lt;/body&gt;"); if(y == -1) y = test.lastIndexOf("&lt;/html&gt;"); if(y == -1) y = content.length; // If no HTML then just grab everything till end return content.slice(x + 1, y); } /** Loads a HTML page Put the content of the body tag into the current page. Arguments: url of the other HTML page to load id of the tag that has to hold the content */ function loadHTML(url, fun, storage, param) { var xhr = createXHR(); xhr.onreadystatechange=function() { if(xhr.readyState == 4) { //if(xhr.status == 200) { storage.innerHTML = getBody(xhr.responseText); fun(storage, param); } } }; xhr.open("GET", url , true); xhr.send(null); } /** Callback Assign directly a tag */ function processHTML(temp, target) { target.innerHTML = temp.innerHTML; } function loadWholePage(url) { var y = document.getElementById("storage"); var x = document.getElementById("displayed"); loadHTML(url, processHTML, x, y); } /** Create responseHTML for acces by DOM's methods */ function processByDOM(responseHTML, target) { target.innerHTML = "Extracted by id:&lt;br /&gt;"; // does not work with Chrome/Safari //var message = responseHTML.getElementsByTagName("div").namedItem("two").innerHTML; var message = responseHTML.getElementsByTagName("div").item(1).innerHTML; target.innerHTML += message; target.innerHTML += "&lt;br /&gt;Extracted by name:&lt;br /&gt;"; message = responseHTML.getElementsByTagName("form").item(0); target.innerHTML += message.dyn.value; } function accessByDOM(url) { //var responseHTML = document.createElement("body"); // Bad for opera var responseHTML = document.getElementById("storage"); var y = document.getElementById("displayed"); loadHTML(url, processByDOM, responseHTML, y); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload