doulu2576 2017-08-28 13:50
浏览 53

有什么选项可以用PHP呈现Javascript“document.write”代码服务器端?

The vendor that we are using for a piece of software has an API that returns raw javascript. The intention is to have a script tag with the API as the source. Our smaller pages have 70+ client-side requests to populate the page with all the required data. The response looks something like this:

document.write("\u003cdiv class=\"cal home_events\"\u003e\u003cdiv class=\"cdate blue\"\u003e\u003cp class=\"date\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23719&information_id=38921&type=&syndicate=syndicate\" target=\"blank\" \u003e8/28\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"cinfo home_event_decs\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23719&information_id=38921&type=&syndicate=syndicate\" target=\"blank\" \u003eGetting Started with Next Generation Classrooms - Incubator Classroom\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv class=\"cal home_events\"\u003e\u003cdiv class=\"cdate blue\"\u003e\u003cp class=\"date\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23756&information_id=39010&type=&syndicate=syndicate\" target=\"blank\" \u003e8/28\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"cinfo home_event_decs\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23756&information_id=39010&type=&syndicate=syndicate\" target=\"blank\" \u003eGmail and Calendar Tips and Tricks\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv class=\"cal home_events\"\u003e\u003cdiv class=\"cdate blue\"\u003e\u003cp class=\"date\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23752&information_id=38995&type=&syndicate=syndicate\" target=\"blank\" \u003e8/28\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"cinfo home_event_decs\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23752&information_id=38995&type=&syndicate=syndicate\" target=\"blank\" \u003eCreating Accessible and Fillable Electronic Forms\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv class=\"cal home_events\"\u003e\u003cdiv class=\"cdate blue\"\u003e\u003cp class=\"date\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23720&information_id=38923&type=&syndicate=syndicate\" target=\"blank\" \u003e8/28\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"cinfo home_event_decs\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23720&information_id=38923&type=&syndicate=syndicate\" target=\"blank\" \u003eGetting Started with Next Generation Classrooms - General\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv class=\"cal home_events\"\u003e\u003cdiv class=\"cdate blue\"\u003e\u003cp class=\"date\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23881&information_id=39283&type=&syndicate=syndicate\" target=\"blank\" \u003e8/28\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"cinfo home_event_decs\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23881&information_id=39283&type=&syndicate=syndicate\" target=\"blank\" \u003eComedySportz Improv Show\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e");

As you can see, a DOM structure is being built and printed to the page where it's called. What I would like to do is render the response serverside for a smoother user experience. We're running PHP on our backend. I'm not sure if V8js is a solution - I can't find any examples where a document.write is used or V8js being used outside the context of React.

  • 写回答

1条回答

  • drruhc4944 2017-08-28 14:17
    关注

    If by this you mean you want to build the js code backend (it'll still load front end though as html pages do with js)

    PHP uses echo

    so:

    echo "whatever you want to output";
    

    or

    $output = "<script>$(document.ready(function(){
    alert('hi');
    });</script>"
    
    echo $output;
    
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况