我目前正在编写一个画廊,直到现在它已经将AJAX和Javascript与xml文件结合起来加载所有图像。现在我来到有趣的部分,我点击一个图像进入内部画廊。 (在第一页上,他们加载了每个会话的一个图像,当按下一个图像时,它将继续到该会话的其余图像。)
我使用AJAX的问题(我确信这也有一个很好的黑客!)是当页面重新加载时,它会自动返回到会话索引,因为没有URL更改。< / p>
我现在所做的,到目前为止,将一个javascript函数放在一个文档中,只加载图库索引。这完美无瑕。
在另一个文件中,我得到了几乎相同的代码,但它加载了加载其余会话图像的函数,但是我没有让它工作。
以下是应加载特定会话图像的脚本。
<script type="text/javascript">
window.onload = displayGallery(<?php echo $_GET['session']; ?>);
</script>
<div id="gallery">
</div>
当我只是javascript和链接中的“onload”它工作得很好,但后来我有问题重新加载页面。现在我只是php包含这个文件和$ _GET变量“displayGallery()”
function displayGallery(sessionAtt)
因此,为了明确这一点,我是垃圾解释的东西,我需要帮助使displayGallery(sessionAtt)在从url抓取基本变量后实际运行。
我对Javascript很新鲜。到目前为止,已完成php中的大多数脚本。
修改
稍微解释一下这个网站:这是我为我的一个朋友制作的画廊。没有其他任何人登录,但她和所有子页面(开始,图库,关于等)都是通过php“include”加载的。
XML:
<gallery>
<session>
<sessionName>Beauty</sessionName>
<path>gallery/beauty/</path>
<item>
<file>_DSC2331.jpg</file>
<name>Picture 1</name>
</item>
<item>
<file>_DSC2339.jpg</file>
<name>Picture 2</name>
</item>
<item>
<file>_DSC2350.jpg</file>
<name>Picture 3</name>
</item>
<date>2011-08-03</date>
</session>
<session>
<sessionName>Red</sessionName>
<path>gallery/red/</path>
<item>
<file>_MG_6227-web.jpg</file>
<name>Picture 1</name>
</item>
<item>
<file>_MG_6235-web.jpg</file>
<name>Picture 2</name>
</item>
<item>
<file>_MG_6240-cropped-web.jpg</file>
<name>Picture 3</name>
</item>
<date>2011-08-03</date>
</session>
</gallery>
js:
function displayGallery(sessionAtt)
{
var xmlDoc = loadDoc("gallery/gallery.xml");
var sessions = xmlDoc.getElementsByTagName("session");
var txt = getGallery(sessions, sessionAtt);
document.getElementById("gallery").innerHTML = txt;
}
function getGallery(sessions, sessionAtt) {
var items = null,
i = 0,
j = 0,
sessionName = "",
link = "",
img = "",
path = "",
file = "",
txt = "";
for (i = 0; i < sessions.length; i++) {
sessionName = sessions[i].getElementsByTagName("sessionName")[0].childNodes[0].nodeValue;
if(sessionName == sessionAtt)
{
items = sessions[i].getElementsByTagName("item");
path = sessions[i].getElementsByTagName("path")[0].childNodes[0].nodeValue;
for (j = 0; j < items.length; j++) {
file = items[j].getElementsByTagName("file")[0].childNodes[0].nodeValue;
link = '<a href="' + path + file + '" rel="lightbox[' + sessionName + ']" title="' + sessionName + '">';
img = '<img src="' + path + file + '" class="thumb" onclick="displayImage();" /></a>';
txt += link + img;
}
}
}
return txt;
}
我试图在函数中设置sessionAtt运行脚本,但它可以工作。
window.onload = displayGallery;
但是当它改为
时window.onload = displayGallery('Red');
手动和使用php ...没什么。
答案 0 :(得分:1)
您的代码对我来说很好。我做的唯一不同的是我使用<body onload="displayGallery('Red')">
而不是window.onload = displayGallery('Red');
正如Martin指出的那样,在页面加载之前使用window.onload
尝试查找<div id='gallery'>
,给出错误: document.getElementById(“gallery”)为空
在调用displayGallery之前,使用<body onload="displayGallery('Red')">
等待页面完成加载。
答案 1 :(得分:0)
我不确定我是否明白你要做什么。但试试这些事情。
**。制作一个javascript变量,它将保存GET ['session']的值。然后使用该变量。它也更容易调试。
**。 insdead of window.onload使一个函数调用它'init'并在body标签中写:
<head>
<script>
function init(){
//displaying gallery or other stuff
}
</script>
</head>
<body onload="init();">
这可确保您的脚本在加载页面后运行。
**如果我说得对,每个用户都有一个会话。在这种情况下(根据经验),在你的php文件的最顶部,甚至在之前
session_start();
答案 2 :(得分:0)
多次阅读你的描述后......
1)GET变量仅在您提供URL中的变量时才有效。的index.php?会话= MODEL1
2)你说后退按钮不起作用,因为页面没有改变,如果正确使用get变量则这是错误的
您的索引页面应该链接到每个会话页面,如下所示:<a href="gallery.php?session=modela">
只有这样才能使用javascript来提取gallery.php上的变量
答案 3 :(得分:0)
对我而言,似乎你应该使用XSLT而不是你做的...将它标记为WIKI,因为它没有回答你的具体问题
答案 4 :(得分:0)
而不是window.onload = displayGallery(<?php echo $_GET['session']; ?>);
使用
window.onload = function() {
displayGallery(<?php echo "'" . $_GET['session'] . "'"; ?>);
};
正如你所说,分配像window.onload = displayGallery;
这样的功能是有效的。在分配window.onload = displayGallery('Red');
时,发生的事情是该功能未附加到window.onload
,但返回的内容是。函数不返回任何内容,因此window.onload = undefined;
。
这也解释了为什么没有发生任何事情,因为没有在onload上调用该函数,而是在浏览器执行脚本时立即调用。
我解决这个问题的方法是通过分配一个函数而不执行它(window.onload = function() { ... }
),然后放置 在其中执行的函数。