Jump to content

Javascript Function Problem


CMellor

Recommended Posts

Hello all,

 

First I'd like to point out that I'm new-ish to Javascript, I don't use it in big chunks, just small.

 

I have made this function, check it out:

 

function callPage(url) {
Spry.Utils.setInnerHTML('blah', 'Loading...');
Spry.Utils.loadURL("GET", url, true, callHandle);
}
function callHandle(req) {
Spry.Utils.setInnerHTML('blah', req.xhRequest.responseText);
}

 

I use an onClick with a Button to call it:

 

<input name="button" type="button" onclick="callPage('spry_form.php?id=14');" value="Click" />

 

and the requested text appears here:

 

<div id="blah"></div>

 

I want to use this function more than once and would want to call a new DIV ID each time, but when I try this function:

 

function callPage(url, div) {
Spry.Utils.setInnerHTML(div, 'Loading...');
Spry.Utils.loadURL("GET", url, true, callHandle);
}
function callHandle(req) {
Spry.Utils.setInnerHTML(div, req.xhRequest.responseText);
}

 

Along with this XHTML:

 

<input name="button" type="button" onclick="callPage('spry_form.php?id=14', 'blah');" value="Click" />

 

When I click the button, it shows the 'Loading...' text, but doesn't request the file, it just says 'Loading...' all the time. As I stated I'm new-ish with Javascript and not sure what I may have done wrong with this function. If anybody can guide me in the right direction, I would be greatly appreciated, and it would help me a lot!

 

Thanks for taking the time to read, and I look forward to a response,

 

Chris.

Link to comment
https://forums.phpfreaks.com/topic/57457-javascript-function-problem/
Share on other sites

in your second function, the usage of 'div' is undefined at that point:

function callPage(url, div) {
    Spry.Utils.setInnerHTML(div, 'Loading...');
    Spry.Utils.loadURL("GET", url, true, callHandle);
}
function callHandle(req) {
    Spry.Utils.setInnerHTML(div, req.xhRequest.responseText);
}

try this:

var saveDivId;
function callPage(url, div) {
    saveDivId = div;   
    Spry.Utils.setInnerHTML(div, 'Loading...');
    Spry.Utils.loadURL("GET", url, true, callHandle);
}
function callHandle(req) {
    // saveDivId used below instead of div
    Spry.Utils.setInnerHTML(saveDivId, req.xhRequest.responseText);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.