0

I've spent a while going through numerous similar questions, but based on the answers given, I'm lead to believe I have this setup correctly - but it's not working, and I'm not sure why.

I'm a newbie with Javascript/jQuery, so it's very possible (or probable!) that I'm missing something completely obvious here.

I have a page with a large 'main' div, and I'm loading content into that using jQuery .load via the navigation. That all works fine. However, some of the target pages are data-heavy, so I'm trying to integrate something in between to indicate to the user that the page is loading. Because there are numerous navigation elements, rather than having multiple functions (i.e one for each navigation element) I'm trying to do it in a single function, like so...

<script type="text/javascript">
function loadPage(pgurl) {
    $('#main').html('<p align="center">Loading...</p>');
    $('#main').load(' +pgurl+ ');
}
</script>

The problem I have is the onclick within the navigation. Prior to this, I had the .load within the onclick (i.e onclick="$('#main').load('/pages/testpage/');") and that worked fine. Now I'm firing the loadPage function via onclick, it's loading a black page (which firebug tells me is the site root).

Here's my onclick code;

<a onclick="loadPage('/pages/testpage/');return false;">Test</a>

I get no errors returned. I can only assume that the loadPage function is getting a zero value, rather than /pages/testpage/ - but I have no idea why!

Pointers much appreciated - much head scratching going on here!

1
  • 1
    $('#main').load(' +pgurl+ '); should be $('#main').load(pgurl); Commented Jan 10, 2013 at 18:01

5 Answers 5

3

It's already a string:

$('#main').load(pgurl);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - of all the things I tried, that wasn't one of them!
2
$('#main').load(' +pgurl+ ');

needs to be

$('#main').load(pgurl);

You should probably write it as one line to prevent the second look up.

function loadPage (pgurl) {
    $('#main').html('<p align="center">Loading...</p>').load(pgurl);
}

1 Comment

Thanks, also for the one line tip!
2

Is that a typo? Try changing:

$('#main').load(' +pgurl+ ');

to

$('#main').load(pgurl);

Comments

2

Because it seem you're not using the pgurl parameter, this thing in the load brackets is string a text of the variable name :)

$('#main').load(' +pgurl+ ');

use that instead

$('#main').load(pgurl);

Maybe you should look at that if you're not familiar with string concatenation http://www.youtube.com/watch?v=n17aX2TdQRk

1 Comment

Thanks - and also thanks for the youtube link, very useful and definitely something I need to brush up on!
1

You have to change following line // you pass string not a variable //

$('#main').load(' +pgurl+ ');

to

$('#main').load(pgurl);

Comments

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.