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
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);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.