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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services