通过附加URL参数来循环URL

时间:2018-08-02 12:26:07

标签: javascript php jquery html

我正在尝试为可通过“ URL”(而不是传统方式)旋转的显示设置静态页面。目前,页面加载时的URL是localcms.com/showDisplay.php?display=3,因为加载页面的链接将display的值传递为3。我要用JavaScript进行的操作是将初始页面加载放在{将第一个JSON元素的{1}}像pageID一样放入URL,然后将持续时间(也是JSON中的值)旋转到下一个,并将下一个JSON元素的pageID放在URL localcms.com/showDisplay.php?display=3&pageID=104

问题是,我的初始页面加载中只有带有localcms.com/showDisplay.php?display=3&pageID=116的传递链接,而我无法获取JavaScript来正确访问JSON,因此每次我访问?display=3时,它都会自动附加第一个pageID,在该元素持续时间内停留在该页面上,然后转到下一个页面。

在页面正文中,我有:

localcms.com/showDisplay.php?display=3

这是从我的主查询中提取的,它以JSON打印该查询中的2条记录:

<?php while($row = mysqli_fetch_assoc($showDisplayResult)){
echo json_encode($row, JSON_PRETTY_PRINT);
}?>

这是我的JavaScript:

{ "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>" }

如何正确访问JSON并在脚本中正确使用它?

更新:

使用此

<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>

我在控制台中得到这个 enter image description here

如果我注释掉当前行并取消注释硬编码的数组,它将在控制台中加载正确的对象,但最初不会将pageID附加到URL

2 个答案:

答案 0 :(得分:0)

修改了您的代码:

<?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>

一切正常。

enter image description here

答案 1 :(得分:-4)

    import { Injectable } from '@angular/core';
    import {Subject} from 'rxjs/Subject';

    @Injectable()
    export class StreetviewService {

    public StreetviewLatSubject= new Subject<any>();
    public StreetviewLonSubject= new Subject<any>();

    constructor() { }

    sendLatToStreetview(latt){
    this.StreetviewLatSubject.next(latt);
    console.log("I have the LAT streetview data: "+latt);
    }

    }