Lee
Lee

Reputation: 999

Passing a URL to a Javascript function

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!

Upvotes: 0

Views: 1367

Answers (5)

kidwon
kidwon

Reputation: 4504

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

Upvotes: 2

Marcin Buciora
Marcin Buciora

Reputation: 449

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

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

to

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

Upvotes: 1

andy magoon
andy magoon

Reputation: 2929

Is that a typo? Try changing:

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

to

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

Upvotes: 2

epascarello
epascarello

Reputation: 207501

$('#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);
}

Upvotes: 2

SeanCannon
SeanCannon

Reputation: 77956

It's already a string:

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

Upvotes: 3

Related Questions