一种从URL获取参数的方法

时间:2013-07-19 08:57:51

标签: php javascript

我有这个问题,我正在努力解决。

我有这个页面lista.php 我在其中加载另一个名为pagination.php

的页面(每x秒刷新一次)

我使用这个脚本:

function LoadContent()
{
    $('#lista').load('pagination.php');
}
$(document).ready(function(){
    LoadContent(); //load contentent when page loads
    setInterval('LoadContent()',30000); //schedule refresh
});

嗯,有这个网址:

lista.php?id=3

我需要pagination.php来获取id,但它不起作用.. pagination.php位于lista.php内...我该如何解决? 我尝试了GET方法..

有什么建议吗? 感谢。

4 个答案:

答案 0 :(得分:1)

要从您的网址id获取lista.php?id=3,您可以使用

var url = window.location.href;
var queryStr = url.substring(url.indexOf("?")+1);
var id = queryStr.split('=')[1];

所以整个代码可能就像:

function LoadContent()
{
    var url = window.location.href;
    var queryStr = url.substring(url.indexOf("?")+1);
    var id = queryStr.split('=')[1];
    $('#lista').load('pagination.php?id=' + id);
}
$(document).ready(function(){
    LoadContent(); //load contentent when page loads
    setInterval('LoadContent()',30000); //schedule refresh
});

答案 1 :(得分:0)

如果您使用AJAX动态加载,那么这是一个不同的URL,因此请将当前查询字符串附加到location.search.substring(url.indexOf("?"))之类的请求。

$('#lista').load('pagination.php' + location.search.substring(url.indexOf("?")));

答案 2 :(得分:0)

我认为你可以使用它:

$('#lista').load('pagination.php?id=<?php echo $_GET['id'] ?>');

答案 3 :(得分:0)

将参数添加到网址,例如:

function LoadContent()
{
    var id = ... <== assign id here
    $('#lista').load('pagination.php?id=' + id);
}
$(document).ready(function(){
    LoadContent(); //load contentent when page loads
    setInterval('LoadContent()',30000); //schedule refresh
});
相关问题