Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to call an external object using javascript (and AJAX?)
    primarykey
    data
    text
    <p>First off, thanks to this community. I'm slowly starting to understand this stuff and I appreciate all the feedback. That said, I come with another question.</p> <p>THE SITUATION: I'm trying to build a new site and I'm trying to use it as a learning experience to use some php and teach myself javascript (note, I'm wanting to stick with js right now and not jump to jquery until I get a better handle on js). I've created a gallery that will have a series of thumbnails at the top that when clicked will populate a div below with artwork, its title and a description.</p> <p>THE SETUP: Because I want to have these called by names and not numbers (such as an array) so they can be interchangeable, I built each as an id's div containing all the components and then I'm calling it to the target location via the script:</p> <pre><code>function changeDiv(id) { var target = document.getElementById('generic'); var id = document.getElementById(id); target.innerHTML = id.innerHTML; } </code></pre> <p>In the body it looks like:</p> <pre><code>&lt;img src="images/thumbs/digital-art/thumb-space-oddity.jpg" id="thumb-space-odity" onclick="callDiv('space-oddity')"/&gt; ... &lt;div class="gallery-frame" id="generic"&gt;&lt;/div&gt; </code></pre> <p>Currently this works but since I have to have a place for all of the divs to be called from I've added a php include in the footer so it loads last but contains all of the images I could possibly need:</p> <pre><code>&lt;div style="display:none;"&gt; &lt;?php include("gallery.php"); ?&gt; &lt;/div&gt; </code></pre> <p>... but from what I understand since this will still mean the page will load all the content that will really slow down the page. So I need a new solution.</p> <p>MY CHALLENGE: First off, is there a way to do this exclusively in javascript? If so I'd gladly do that!</p> <p>Assuming I cannot, I have started looking into AJAX to see how it works and while it's starting to make nominal sense (still new to javascript so I only get half of it) and between w3schools.com and the <a href="https://stackoverflow.com/questions/5092352/relative-path-problems-in-javasvript-ajax-call">stackoverflow topic</a> I think i have a general idea of what I need to do but it's still not working.</p> <p>THE PROBLEM: Based on the above stackoverflow comment I attempted to recreate the example function in my own instance and I have set it up as the following:</p> <pre><code>&lt;script type="text/javascript"&gt; function createXMLHttpRequest() { try { return new XMLHttpRequest(); } catch (e) { alert('XMLHttpRequest not working'); } try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { alert('Msxml2.XMLHTT not working'); } try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert('Microsoft.XMLHTTP not working'); } alert("XMLHttpRequest not supported"); return null; } function callDiv(id) { var xmlHttpReq = createXMLHttpRequest(); var url = "gallery.asp?ID=" + id; xmlHttpReq.open("GET", url, true); xmlHttpReq.send(); } &lt;/script&gt; </code></pre> <p>From what I've read I left the first section alone as that sets up the function (removing the null and prompts as I don't need them) and trying to have the callDiv function run the same as the previous script I was using. I have also saved out the gallery.php page as an .asp page but should I have left it as php? </p> <p>Did it have to be .ashx?</p> <p>And since I don't know the file it was being called from I don't know what the '?REASON' that was used in the original example meant. I tried ?ID thinking I was telling it to look for an id. Is that wrong, or unnecessary?</p> <p>Thanks!</p> <p>-Chris</p>
    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