hey all,

i found a way to click a link and load it into a div. the only problem is that i keep getting the "AHA error" from the if statement in my div.

i'm not sure if it is me or the coding but i'll post what i'm using in jscript here:

function ahah(url, target) {
  document.getElementById(target).innerHTML = ' Fetching data...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function ahahDone(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}

function load(name, div) {
	ahah(name,div);
	return false;
}

and this is what i'm using on the html side

<area shape="rect" coords="255,519,399,558" href="history.html" onclick="load('history.html','leftmain');return false;">

Please let me know where i'm going wrong. i don't think it's because i'm using an image map (i know... ol' skoolin it) but i'm not sure where to go for help.

thanx in advance.

Dani AI

Generated

Quick clarification based on the thread: ’s iframe workaround is fine when you need an entire page (and its scripts) to run in its own document. When you want to pull a fragment into an existing page, browsers will usually deliver the HTML via AJAX but will not reliably run the <script> tags when you simply set innerHTML. That explains ’s “page loads but the script doesn’t execute.”

A practical pattern that works reliably:

  • Fetch the HTML (fetch/XHR).
  • Parse it into a document (DOMParser) and copy only the fragment you want (usually doc.body or a specific container).
  • Extract any <script> elements from the parsed document and append new <script> elements to the live document so the browser executes them. For external scripts, insert and wait for onload before running the next script to preserve order.

Example implementation (modern browsers):

function loadInto(targetSelector, url) {
  const target = document.querySelector(targetSelector);
  return fetch(url, { credentials: 'same-origin' })
    .then(r => { if (!r.ok) throw new Error(r.status); return r.text(); })
    .then(html => {
      const doc = new DOMParser().parseFromString(html, 'text/html');
      target.innerHTML = doc.body ? doc.body.innerHTML : html;
      const scripts = Array.from(doc.querySelectorAll('script'));
      return scripts.reduce((p, s) => p.then(() => new Promise((res, rej) => {
        const ns = document.createElement('script');
        if (s.src) { ns.src = s.src; ns.onload = res; ns.onerror = rej; document.head.appendChild(ns); }
        else { ns.textContent = s.textContent; document.head.appendChild(ns); res(); }
      })), Promise.resolve());
    });
}

Notes and cautions:

  • Scripts that use document.write() or expect to be in a top-level document won’t behave correctly when injected; those are better loaded in an iframe.
  • AJAX fetches are subject to same-origin/CORS rules.
  • Re-inserting scripts from untrusted sources is an XSS risk—sanitize server output if needed.
  • For simpler cases, libraries (or server-side templating) can help; for full pages, iframe remains the easiest option.

Further reading: Element.innerHTML (https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML) and DOMParser (https://developer.mozilla.org/en-US/docs/Web/API/DOMParser).

got it guys. just decided to use an iframe

Gr8!!! The script works great. i was able to load the history.html in a specific div

onclick="load('history.html','div')"

History.html
=============
<html>
<head><title></title></head>
<body>
<p>TEST</p>
<script language="javascript">
document.write("Test");
</script>
</body>
</html>


The history page loads successful but the script under the page does not execute.

Please Help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.