![](/img/trans.png)
[英]Cycle page URLs by appending json value to URL and refresh on interval
[英]cycle URLs by appending URL parameters
我正在尝试为显示设置静态页面,该页面可以通过“ URL”旋转,但传统上不能。 现在,页面加载时的URL是localcms.com/showDisplay.php?display=3
因为加载页面的链接将display
的值传递为3。我想用我的JavaScript进行的是初始页面加载所述pageID
第一JSON元件的成像URL localcms.com/showDisplay.php?display=3&pageID=104
然后持续时间(也来自JSON值)后转动到下一个,并把下一个JSON元件的的pageID在URL localcms.com/showDisplay.php?display=3&pageID=116
。
问题是,我的初始页面加载中只有带有?display=3
链接,并且我无法获取JavaScript来正确访问JSON,因此无论何时我访问localcms.com/showDisplay.php?display=3
它都会自动追加第一个pageID,在该元素持续时间内停留在该页面上,然后转到下一个页面。
在页面的正文中,我有:
<?php while($row = mysqli_fetch_assoc($showDisplayResult)){
echo json_encode($row, JSON_PRETTY_PRINT);
}?>
这是从我的主要查询中提取的,它以JSON打印该查询中的2条记录:
{ "pageID": "104",
"page_type_id": "1",
"display_id": "3",
"slide_order": null,
"duration": "56",
"active": "1",
"background_img": null,
"panel_id": "96",
"panel_type_id": "1",
"page_id": "104",
"cont_id": "148",
"contID": "148",
"content": "\r\n\r\n\r\n<\/head>\r\n\r\nThis is full content<\/p>\r\n<\/body>\r\n<\/html>" }
{ "pageID": "116",
"page_type_id": "1",
"display_id": "3",
"slide_order": null,
"duration": "54",
"active": "1",
"background_img": "images\/BG_spring.svg",
"panel_id": "113",
"panel_type_id": "1",
"page_id": "116",
"cont_id": "165",
"contID": "165",
"content": "\r\n\r\n\r\n<\/head>\r\n\r\nThis background should be green<\/p>\r\n<\/body>\r\n<\/html>" }
这是我的JavaScript:
<script type="text/javascript">
// encode your php array to json
let obj = <?php echo json_encode($row); ?>;
let params = new URL(document.location).searchParams;
params.set("pageID", obj.pageID);
params.set("display", obj.display_id);
let url = window.location.href.split('?')[0];
let nextURL = url + "?" + params.toString();
window.setTimeout(function () {
window.location.href = nextURL;
}, obj.duration * 1000);
console.log(obj);
</script>
如何正确访问JSON并在脚本中正确使用它?
更新:
使用这个
<?php
// $row = '{ "pageID": "104",
// "page_type_id": "1",
// "display_id": "3",
// "slide_order": null,
// "duration": "56",
// "active": "1",
// "background_img": null,
// "panel_id": "96",
// "panel_type_id": "1",
// "page_id": "104",
// "cont_id": "148",
// "contID": "148",
// "content": "\r\n\r\n\r\n<\/head>\r\n\r\nThis is full content<\/p>\r\n<\/body>\r\n<\/html>" }
// ';
$row = mysql_fetch_assoc($showDisplayResult);
?>
</div>
<script type="text/javascript">
// encode your php array to json
let obj = <?php echo json_encode($row); ?>;
obj = JSON.parse(obj);
let params = new URL(document.location).searchParams;
params.set("pageID", obj.pageID);
params.set("display", obj.display_id);
let url = window.location.href.split('?')[0];
let nextURL = url + "?" + params.toString();
window.setTimeout(function () {
window.location.href = nextURL;
}, obj.duration * 1000);
console.log(obj);
</script>
如果我注释掉当前行并取消注释硬编码数组,它将在控制台中加载正确的对象,但最初不会将pageID附加到URL
修改您的代码:
<?php
$row = '{ "pageID": "104",
"page_type_id": "1",
"display_id": "3",
"slide_order": null,
"duration": "56",
"active": "1",
"background_img": null,
"panel_id": "96",
"panel_type_id": "1",
"page_id": "104",
"cont_id": "148",
"contID": "148",
"content": "\r\n\r\n\r\n<\/head>\r\n\r\nThis is full content<\/p>\r\n<\/body>\r\n<\/html>" }
';
?>
<script type="text/javascript">
// encode your php array to json
let obj = <?php echo json_encode($row); ?>;
obj = JSON.parse(obj);
let params = new URL(document.location).searchParams;
params.set("pageID", obj.pageID);
params.set("display", obj.display_id);
let url = window.location.href.split('?')[0];
let nextURL = url + "?" + params.toString();
window.setTimeout(function () {
window.location.href = nextURL;
}, obj.duration * 1000);
console.log(obj);
</script>
一切正常。
<script type="text/javascript"> // encode your php array to json taking php to a json file let obj = <?php echo json_encode($row); ?>; enter code here obj = JSON.parse(obj); let parameter = new URL(document.location).searchParams; paramameter.set("pageID", obj.pageID); parameter.set("display", obj.display_id); let url = window.location.href.split('?')[0]; let nextURL = url + "?" + params.toString(); window.setTimeout(function () { window.location.href = nextURL; }, obj.duration * 1000); console.log(obj); </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.