将JSON值发送到URL

时间:2018-08-07 01:13:07

标签: javascript jquery json refresh

我有一个JSON对象:

[{"page":"Test",
    "seconds":"56",
}]

并且我正在尝试使用Javascript在加载“页面”值时将其加载到我的URL中,并且在“秒”值中的秒数后,刷新页面。

当我单击链接<a href="testpage.php?screen=1"></a>时,我不希望URL永远只是testpage.php?screen=1,但我希望页面加载立即将'page'的值附加到URL,如下所示:

testpage.php?screen=1&page=Test

这是我到目前为止拥有的脚本:

<script type="text/javascript">
let obj = <?php echo $object; ?>;//This is my JSON object, already encoded and parsed

let params = new URL(document.location).searchParams;
params.set("page", obj[0].page);

window.setTimeout(function () {
    window.location.href = /*not sure here*/;
}, obj.seconds * 1000);

</script>

非常感谢您提供完成脚本的帮助,我不确定如何完成此脚本以执行适当的功能。

更新:

尝试根据时间间隔显示和隐藏内容

如果我的JSON的元素具有一个页面ID,则可以正常工作,但是如果第1页有2个元素,而第2页有2个元素,则不起作用。

我想在第一页显示元素,在间隔之后显示下一个元素

let obj = [{
    "pageID": "93",
    "page_type_id": "2",
    "display_id": "2",
    "duration": "74",
    "background_img": "images\/bg_rainbow.svg",
    "panel_id": "86",
    "panel_type_id": "2",
    "cont_id": "138",
    "contID": "138",
    "content": "\r\n\r\n\r\n<\/head>\r\n\r\nLeft 93<\/p>\r\n<\/body>\r\n<\/html>"
  },
  {
    "pageID": "93",
    "page_type_id": "2",
    "display_id": "2",
    "slide_order": null,
    "duration": "74",
    "background_img": "images\/bg_rainbow.svg",
    "panel_id": "87",
    "panel_type_id": "3",
    "cont_id": "139",
    "contID": "139",
    "content": "\r\n\r\n\r\n<\/head>\r\n\r\nRight 93<\/p>\r\n<\/body>\r\n<\/html>"
  },
{
    "pageID": "94",
    "page_type_id": "2",
    "display_id": "2",
    "slide_order": null,
    "duration": "75",
    "background_img": "images\/bg_rainbow.svg",
    "panel_id": "88",
    "panel_type_id": "3",
    "cont_id": "140",
    "contID": "140",
    "content": "\r\n\r\n\r\n<\/head>\r\n\r\nRight New<\/p>\r\n<\/body>\r\n<\/html>"
  }
];
let counter = 0;

var fullContent = document.getElementById('fullContent');
var leftContent = document.getElementById('leftContent');
var rightContent = document.getElementById('rightContent');

var fullColumn = document.getElementById('fullColumn');
var leftColumn = document.getElementById('leftColumn');
var rightColumn = document.getElementById('rightColumn');



setInterval(() => {
  const currentJSONobject = obj[counter];

  if (currentJSONobject.page_type_id == 1) {

    leftColumn.style.display = "none";
    rightColumn.style.display = "none";
    

    if (currentJSONobject.panel_type_id == 1) {

      fullContent.innerHTML = currentJSONobject.content;
    }

    //If half page    
  } else if (currentJSONobject.page_type_id == 2) {

    fullColumn.style.display = "none";


    if (currentJSONobject.panel_type_id == 2) {

      leftContent.innerHTML = currentJSONobject.content;

    } else if (currentJSONobject.panel_type_id == 3) {

      rightContent.innerHTML = currentJSONobject.content;
    }
  }

  $('#middle').css('background-image', 'url(' + currentJSONobject.background_img + ')');

  counter += 1;
  if (counter === obj.length) {
    counter = 0;
  }

}, 1500)
console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row middle">
  <!-- Full Page Divs -->
  <div class="col-lg-12" id="fullColumn">
    <div class="fullContent" id="fullContent" style="height: 100%; ">
    </div>
  </div>
  <!-- End Full Page Divs -->

  <!-- Half Page Divs -->
  <div class="col-lg-6 leftColumn">

    <div class="leftContent" id="leftContent" style=" height: 100%; ">

    </div>
  </div>

  <div class="col-lg-6 rightColumn">

    <div class="rightContent" id="rightContent" style=" height: 100%; ">

    </div>

  </div>
  <!-- End Half Page Divs -->
</div>

2 个答案:

答案 0 :(得分:1)

您需要将新的URL对象保存在变量中,以便可以整体进行修改。

let newUrl = new URL(document.location);
newUrl.searchParams.set("page", obj[0].page);
window.setTimeout(function() {
    window.location.href = newUrl.href;
}, obj[0].seconds * 1000);

答案 1 :(得分:1)

我希望以下代码对您有用。

以下代码已编写完毕,紧记obj数组的最后一个元素将用于重新加载。

<?php //testpage.php
    $arr = array(
        array("page"=>"Test","seconds"=>2),
        array("page"=>"Dummy","seconds"=>3),
    );

    $object = json_encode($arr);
?>
<h1>This is test page </h1>

<script type="text/javascript">
    function reloadPage() {
        var obj = <?php echo $object?>;
        alert("Page is going to reload in "+obj[obj.length-1].seconds+" seconds");
        let newUrl = new URL(document.location);
        newUrl.searchParams.set("page", obj[obj.length-1].page);
        window.setTimeout(function() {
        window.location.href = newUrl.href;
        }, obj[obj.length-1].seconds * 1000);
    }

    var url = new URL(document.location);
    if(!url.searchParams.get("page")){
        window.addEventListener('load', reloadPage, false );
    }
</script>