Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Silly jQuery Can't use AJAX properly.
Old 09-29-2010, 09:21 PM Silly jQuery Can't use AJAX properly.
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
..Or something.

Maybe I'm just doing it wrong. Here's the code:

Code:
<div id="top"><a href="index.php">Home</a> | <a href="otherpage.php">Other Page</a></div>
<div id="container">
<script type="text/javascript">
$('#top a').click(function(){  
        var pageURL=$(this).attr('href');
    $('#container').fadeOut(100);
    $('#container').load(pageURL '#container');
    $('#container').fadeIn(400);
    return false;
});
</script>
</div>
So what I want it to do is when you click a link in the #top div, it would fade out the current page, load up the URL you clicked on (but just the #container div) and fade back in with the new content.

Problem is, it doesn't want to load up just the container. Either you have to load the entire page, not just the contents of the container div. But when I run that code, it also displays the #top div, indicating that it's loading the entire page, not just the container.

How can I get it to just load the container? I can do what I want it to by using
Code:
    $('#container').load('otherpage.php #container');
but as soon as I put in the pageURL variable it decides not to work :\.

I've tried everything, seriously. I've been at this for a half hour now. Somebody please help :P

Thanks,

-PG
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-29-2010, 11:16 PM Re: Silly jQuery Can't use AJAX properly.
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
There's a couple of holes I can poke in your logic.

1. You're asking jQuery to read the contents of otherpage.php#container into #container, same #id.

2. Your jQuery snippet handling all this in IN thispage#container!

Try using a different ID in otherpage.php and move your jQuery snippet out of thispage#container.

[edit] Don't forget to wrap your snippet in a call to jQuery().ready(function() { }); [/edit]
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.

Last edited by metho; 09-29-2010 at 11:18 PM.. Reason: after thought
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 09-30-2010, 10:35 AM Re: Silly jQuery Can't use AJAX properly.
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
Silly you! You forgot a comma in this line, and your script is crashing, not getting to the "return false", and so the link is not being stopped (you're being redirected... check your browser url as it changes)

$('#container').load(pageURL, '#container'); // <--- note the comma after pageURL.
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Old 09-30-2010, 03:37 PM Re: Silly jQuery Can't use AJAX properly.
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Ah, I've tried the comma thing, I noticed it too.

I have moved the script out of container, and have put it in right after #top. With the comma it works, but it still doesn't grab just #container. Without the comma it crashes.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 09-30-2010, 08:03 PM Re: Silly jQuery Can't use AJAX properly.
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
Right, I noticed that it's not parsing out the container bit. A little research on jQuery.com shows that it should be the following:

$('#container').load(pageURL + ' #container');

This tells the load function to pull 'ABC.html #container', which is properly referencing the fragment in the URL.
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Old 09-30-2010, 08:06 PM Re: Silly jQuery Can't use AJAX properly.
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Hmm, I've tried that too. I'll try it again and check back with you later
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 10-01-2010, 01:58 AM Re: Silly jQuery Can't use AJAX properly.
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
From the notes for jQuery.load():-
Quote:
When this method executes, it retrieves the content of ajax/test.html, but then jQuery parses the returned document to find the element with an ID of container. This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded.
Do you think jQuery could to insert the other #container and contents into an element by the same id name? It's an obvious DOM conflict and will throw jQuery targeting out the window. As I mentioned before, change either #content id to some other unique name.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 10-01-2010, 10:07 AM Re: Silly jQuery Can't use AJAX properly.
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
metho, it's not a DOM conflict, because they're separate Document instances. His code should work fine.
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Old 10-01-2010, 10:43 AM Re: Silly jQuery Can't use AJAX properly.
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
smoseley have you tried to get jQuery to produce invalidly nested element id's?

Does not work:
Code:
<script type="text/javascript">
var pageVar = 'jquery.load.htm';
jQuery('#content').load(pageVar + ' #content');
</script>
The resulting (invalid) html would be:
Code:
<div id="content"><div id="content">External page #content html</div><div>
The following works fine; the only difference is changing the ids to unique values (likewise in the webpages), as required in a healthy dom.
Code:
<script type="text/javascript">
var pageVar = 'jquery.load.htm';
jQuery('#target').load(pageVar + ' #source');
</script>
Physicsguy, you might also have to use 'jQuery' instead of '$', depends on the version of jQuery you're using.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 10-01-2010, 03:37 PM Re: Silly jQuery Can't use AJAX properly.
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
Yes, I tried it, and the following DOES work for me...

Further, I've added animation overflow handling to make the code function as intended:

index.html content
HTML Code:
<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        var loader;
        $('#top a').bind("click", function(){  
            var outTime = 100;
            var inTime = 400;
            var pageURL = $(this).attr('href');
            $('#container').stop(true, true);
            $('#container').fadeOut(outTime);
            window.clearTimeout(loader);
            loader = window.setTimeout(function() {
                $('#container').load(pageURL + ' #container');
                $('#container').fadeIn(inTime);
                loading = false;
            }, outTime);
            return false;
        });
    });
    </script>
</head>
<body>
    <div id="top">
        <a href="index.html">Home</a> | <a href="otherpage.html">Other Page</a>
    </div>
    <div id="container">
        <h1>This is my homepage</h1>
        <p>What do you think???</p>
    </div>
</body>
</html>
otherpage.html content
HTML Code:
<html>
<head>

</head>
<body>
    <div id="top">
        <a href="index.html">Home</a> | <a href="otherpage.html">Other Page</a>
    </div>
    <div id="container">
        <h1>This is my OTHER page</h1>
        <p>What does your momma think dude?!!</p>
    </div>
</body>
</html>
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE

Last edited by smoseley; 10-01-2010 at 03:38 PM..
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Old 10-01-2010, 03:39 PM Re: Silly jQuery Can't use AJAX properly.
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Thanks very much you guys! That's what I needed, and thanks for fixing the animation bug, smoseley. And thanks metho for your help
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 10-01-2010, 03:44 PM Re: Silly jQuery Can't use AJAX properly.
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
metho, I see what you're saying now... the following selector fixes the double #container problem by selecting the decendents in the target doc:

$('#container').load(pageURL + ' #container *');
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Old 10-04-2010, 07:05 AM Re: Silly jQuery Can't use AJAX properly.
CSM
CSM's Avatar
Front-End Developer

Posts: 297
Name: Michael Pehl
Location: Palma de Mallorca
Trades: 0
Quote:
Originally Posted by metho View Post
Physicsguy, you might also have to use 'jQuery' instead of '$', depends on the version of jQuery you're using.
That's not correct.

If you use jQuery you are using the library in noConflict mode, e.g. like this:

[code]
jQuery.noConflict();

jQuery(document)ready(){

// crap code here k?

});

Reference : http://api.jquery.com/jQuery.noConflict/
__________________
Chief Web Officer / Front-End Developer / System Engineer

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

Last edited by CSM; 10-04-2010 at 07:07 AM..
CSM is offline
Reply With Quote
View Public Profile Visit CSM's homepage!
 
Old 10-04-2010, 09:56 AM Re: Silly jQuery Can't use AJAX properly.
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
Knit picker.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 10-04-2010, 11:28 AM Re: Silly jQuery Can't use AJAX properly.
CSM
CSM's Avatar
Front-End Developer

Posts: 297
Name: Michael Pehl
Location: Palma de Mallorca
Trades: 0
It's not a good idea to give people wrong informations if they want to learn!

That's how I think. If you think I am a "Knit picker", okay... then I am a "Knit picker" for you.

But I am right, and if he (one day) needs to know the noConflict mode... then he will remember my post...

If not ... Google is your friend
__________________
Chief Web Officer / Front-End Developer / System Engineer

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
CSM is offline
Reply With Quote
View Public Profile Visit CSM's homepage!
 
Old 10-04-2010, 11:44 AM Re: Silly jQuery Can't use AJAX properly.
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
Yeah - ur right. But it's early am and I'm tired, sick of working, and the post ur picking at was another midnight oil burning night. The point I wanted to make was to use jQuery instead of $ in case of a conflict. Instead it came out that way due to the cooked noodle state I'm in some late nights.

Not in the carebear mood tonight.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 10-05-2010, 07:01 PM Re: Silly jQuery Can't use AJAX properly.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
<pedantic>
And anyway it's nit picker ("nit" being a term for the eggs of head lice)
</pedantic>

Knit picker would maybe imply a nervous habit of pulling at strands in woollen clothing.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-05-2010, 08:47 PM Re: Silly jQuery Can't use AJAX properly.
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
you have way too much spare time.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 10-05-2010, 08:56 PM Re: Silly jQuery Can't use AJAX properly.
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Quote:
Originally Posted by chrishirst View Post
<pedantic>
And anyway it's nit picker ("nit" being a term for the eggs of head lice)
</pedantic>

Knit picker would maybe imply a nervous habit of pulling at strands in woollen clothing.

That was pure awesome. Grammar Nazis FTW!
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 10-06-2010, 04:59 AM Re: Silly jQuery Can't use AJAX properly.
CSM
CSM's Avatar
Front-End Developer

Posts: 297
Name: Michael Pehl
Location: Palma de Mallorca
Trades: 0
ROFL ... that was fun
__________________
Chief Web Officer / Front-End Developer / System Engineer

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
CSM is offline
Reply With Quote
View Public Profile Visit CSM's homepage!
 
Reply     « Reply to Silly jQuery Can't use AJAX properly.

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.07297 seconds with 12 queries