开发者

Passing Parameters to a jQuery 'Load'

I have some script on my page which is:

$('#divMenuHolder').load('../menu.html');

However I also need to be able to pass some parameters which are acted on the menu.html page.

I've tried the following:

$('#divMenuHolder').load('../menu.html?opt1=test&opt2=test2');

However when I come to get the parameters using the following function:

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i开发者_Go百科].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

It returns nothing because it's trying to get the parameters of the page itself, and not the page I'm loading into DIV.

Can anyway assist in guiding me to what I want please?


This works for me

$('#semester').load("restricted/getSemester.php?courseid=" + $("#course").val());

where #course is id of input field.

OR

$('#subject').load("restricted/getSubject.php?courseid=0&semester=0");

I hope this helps u :)


According to this jQuery load() documentation example:

Example: pass arrays of data to the server.

$("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );

Function definition:

.load( url, [data,] [complete(responseText, textStatus, XMLHttpRequest)] )

url A string containing the URL to which the request is sent.

data A map or string that is sent to the server with the request.

complete(responseText, textStatus, XMLHttpRequest) A callback function that is executed when the request completes.


Try following

var url_to_load = '../menu.html?opt1=test&opt2=test2';
$('#divMenuHolder').load(url_to_load);

function getUrlVars(url_to_load)
{
    var vars = [], hash;
    var hashes = url_to_load.slice(url_to_load.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}


I ran in to a similar problem. Was Just wondering if you have found the solution. I found a workaround but its not suitable for my application.

One of the options is to set the cookie before you load the URl and access the cookie in the seperate html page. But cookies can be disabled so you have to be careful on that part.Also if your separate html is going to reusable , then you need to figure out the perfect solution , cookies are not useful in that case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜