1

I am confused, i included the jQuery 1.7 library before executiong this script:

var rq;

function request(localTarget, parameter)
{
    $("#" + localTarget).text("Laden...");
    rq = new XMLHttpRequest();
    rq.open("GET", parameter, false);
    rq.onreadystatechange = auswerten(localTarget, parameter);
}

function auswerten(target, site)
{   
    if (rq.readyState == 4 && rq.status == 200) {
        $("#" + target).text(rq.responseText);
    } else {
        $("#" + target).text("Wert konnte nicht geladen werden");
    }
}

But the Text won't show up, i tried everything, but it won't work!

the Html code i want to insert the text goes like this:

<table width='100%' cellpadding='4' cellspacing='0'>
<tr>
  <td class='tablesubheader' width='50%'>Typ</td>
  <td class='tablesubheader' width='50%'>Wert</td>
</tr>

<tr>
<td>Geld heute</td>
<td><div id="money_today">Test</div></td>
</tr>

<tr>
<td>Geld Monat</td>
<td id="money_month"></td>
</tr>

<tr>
<td>Klicks heute</td>
<td id="klicks_today"></td>
</tr>

<tr>
<td>Klicks Monat</td>
<td id="klicks_month"></td>
</tr>

</table>
</td>

The method above is called like this:

<script type="text/javascript">
$(window).load(function() {
    request('money_today','<?php echo $link1; ?>');
    request('money_month','<?php echo $link2; ?>');
    request('klicks_today','<?php echo $link3; ?>');
    request('klicks_month','<?php echo $link4; ?>');
});
</script>
4
  • Where are you calling request method in your script? Commented Oct 13, 2012 at 18:06
  • above ajax code will work only in IE. Commented Oct 13, 2012 at 18:09
  • @MaximilianWalter I modified your code I am using jQuery load. Commented Oct 13, 2012 at 18:13
  • i should say that localTarget is a div element and parameter is a url from another website Commented Oct 13, 2012 at 18:20

3 Answers 3

1

Move your code inside $(document).ready and use .html instead of .text.

modified code: jquery load

    function request(localTarget, parameter)
    {
        $("#" + localTarget).("Wert konnte nicht geladen werden");
        $("#" + localTarget).load(parameter);
    }

Note: you should provide function reference to rq.onreadystatechange load not return value of function. auswerten(localTarget, parameter); will assign undefined to onreadystatechange .

Sign up to request clarification or add additional context in comments.

10 Comments

are you getting any exception?
erm... lets see: my browser cant prase $('#divelement') to an object. When i just call $('#divelement').html('BlaBla'); nothing apears in the given element, its confusing
Have checked the element data using developer tools. might be ids are not correct.
you can't because it has administrative functions. But i can give you the code. And the error that is thrown is: -- [20:44:21.114] TypeError: $("#test") is null @ www.*****.biz/manage/hotel/de/housekeeping/index/p/home:2
$("#test") can not be null as even element is not present $("#test") will be a object. are you sure $ is jQuery. are you using any other library which use $.
|
0

It's possible the problem is that your code is running before the elements you're trying to deal with exist on the page: i.e. before the pages is loaded. Try putting that code inside the following:

$(document).ready(function() {
    // Your code here
});

Comments

0
rq.onreadystatechange = auswerten(localTarget, parameter);

You're calling the function right here. You don't what that - the browser should call the function when the response has arrived:

// pass a function (that you don't call), which in turn calls `auswerten`
rq.onreadystatechange = function() {
  auswerten(localTarget, parameter);
};

Anyway, jQuery has a built-in $.ajax function that has cross-browser normalization and is quicker to use.

8 Comments

i hate javascript, so please just tell me how to load a text with ajax from a url and paste it to a div element
@Maximilian Walter: In jQuery that can be done with just $("#id").load("foo.html").
Ok i got this element on my site:<div id="klicks_month"></div>
And i execute this code now: $("#klicks_month").load('<?php echo $link4; ?>'); But it sill won't work!
@MaximilianWalter: I can only guess, so: 1) Use $(document).ready, 2) make sure the ID is really correct and the element exists, 3) make sure the file you're loading exists, 4) use a console to check for errors.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.